1,897 questions
0 votes
2 answers
78 views
Dropping optional borrow in structure
I am writing a scripting language in rust, and I'm pretty new to the language. When I compile source into byte code I move the compiled symbols (Chunk) out of the function. I am also lazy and don't ...
3 votes
0 answers
77 views
Returning mutable references and lifetime in rust [duplicate]
I am trying to develop a linear algebra library in rust. So, as part of it I am writing code that iterates through a n-dimensional array and returns mutable references. It's like writing a view for a ...
4 votes
2 answers
123 views
Rust global variable lifetime safety
This is a pattern I want to be able to use in my code to have global variables that contain non-static references. Is it memory safe? If not, how can I make it so? use std::cell::Cell; pub struct ...
5 votes
1 answer
180 views
Why does the presence of the drop method cause the borrow checker to become unhappy?
While trying to write an API inspired by the structure of std::thread::scope, I ran across the following problem. This function compiles fine (playground): use std::marker::PhantomData; pub struct ...
5 votes
1 answer
132 views
Rust specifying a trait and associated type are valid for the lifetime inside a function
I am using the winnow crate to write a parser and am struggling with adding tests due to lifetime conflicts. The basic setup I have is a stateful stream: // The primary stream type type SStream<'i, ...
1 vote
1 answer
119 views
Why does 'borrowed valued does not live long enough' error appear when storing value and its reference in a struct?
I'm aware it is not much possible nor recommended to have both a field that owns a value, and another one that stores a reference to the same value in a struct. I was experimenting a bit with a more ...
0 votes
1 answer
141 views
how to avoid "borrow of moved value" with subpattern match
Edit: added a second part of the question below. I am getting the error "borrow of moved value" here, but the thing being moved is an &mut Containee, so I didn't expect it to cause a ...
2 votes
1 answer
68 views
Handling of async callback that mutates the environment
I'm trying to create a little utility that simplifies the execution of repetitive work. This would be almost trivial in memory managed languages, but gets unnervingly complex in Rust. I'd call myself ...
4 votes
2 answers
189 views
Understanding how Rust elides lifetimes with mutable references
I want to understand lifetimes and the elision rules better. Suppose we want to extend a Vec<i32> (really Vec<&i32>) fn extend_vector( v: &mut Vec<&i32>, x: &...
5 votes
1 answer
79 views
Why does this code that moves a closure into another closure and returns both even compile?
I have the following code: fn test() -> (impl FnMut(&mut u8), impl FnMut(&mut u8)) { let a = |v: &mut u8| {*v = 0}; let b = move |v: &mut u8| { a(v); println!("{}&...
1 vote
2 answers
102 views
Rust borrow checker: "cannot borrow `...` as immutable because it is also borrowed as mutable [E0502] immutable borrow occurs here"
I wanted to implement the Merge Sort algorithm, but I am having trouble with the borrow checker rules. The compiler gives me the hint: cannot borrow *lst as immutable because it is also borrowed as ...
1 vote
1 answer
66 views
How to fix this lifetime-related error happen when using the Fn trait?
Context I'm trying to learn Rust by just reading the book and working through the problems in Advent of Code (the 2024 edition). Just to make it harder, I decided to practice TDD while I do it. To ...
1 vote
2 answers
89 views
Comparing Swift's and Rust's function signatures
I want to check my understanding of how Swift and Rust handle function parameters. From what I’ve seen, Swift has two main parameter modes: Pass by value, immutable: f(x: T) and Pass by reference, ...
4 votes
1 answer
77 views
If the return value of a function has the same lifetime as one of the arguments, then the return value is considered a borrow of the argument?
Where in Rust's specification does it say that if the returned reference of a function has the same lifetime as one of the reference arguments, then the returned reference is considered a borrow of ...
0 votes
0 answers
74 views
Non-bitwise move or `Move` trait? [duplicate]
In Rust, how can I define&manipulate objects that cannot be just copied bit-by-bit when they're moved? For example, an object that contains a relative pointer (i.e. a pointer whose target is ...