Questions tagged [lifetime]

A variable's life-time is the time during which the variable is bound to a specific memory location. The life time starts when the variable is allocated and ends when it is deallocated.

Most of the time a lifetime is a synonym for the scope, for example in this code:

void foo()
{
    int x = 0; // lifetime of `x' begins here ──┐
    //                                          │
    printf("%d\n", x); //                       │
} // and finishes here ─────────────────────────┘

Rust:

The lifetime is a key concept in : it is a construct the compiler (also called the ) uses to ensure all borrows are valid.

Links:

2138 questions
0
votes
1 answer

How to move generic typed parameter to an async move block?

I'm trying to write something in Rust (which I love but don't have much experience yet) and found a hurdle I have no idea how to sort out. My intention is to spawn an asynchronous task passing an initial state as parameter. The asynchronous task…
helios
  • 13,574
  • 2
  • 45
  • 55
0
votes
0 answers

Why is a variable not borrowed for static if it is dropped at the end of the main function in Rust?

I am trying to pass a closure which captures a local variable: fn main() { /* snip */ let COINT = some_function_call(); /* snip */ hermes.with_task( u32::MAX, 1, 1, Box::new( |hermes| { let mut rng =…
Plegeus
  • 139
  • 11
0
votes
1 answer

How to pass a closure to a function from a method on a struct that has a lifetime parameter in Rust?

I have a struct Foo with a lifetime parameter 'a: struct Foo<'a> { /* snip */ } I want to pass a closure (which encloses some of Foo's fields) to an arbitrary function, for example: impl Foo<'_> { // ... pub fn do_something(&mut self) { …
Plegeus
  • 139
  • 11
0
votes
0 answers

Returning a reference to a function scoped value works for some reason in rust, need explanation

I am using rustc 1.70.1 I have a function which I thought would crash, but it doesn't: fn testset<'a> () -> std::collections::HashSet<&'a i64> { let mut set = HashSet::new(); set.insert(&123456); set } fn main() { let test =…
Naphat Amundsen
  • 1,519
  • 1
  • 6
  • 17
0
votes
0 answers

Rust error: cannot assign to `self.position` because it is borrowed

I am attempting to create a struct in Rust that has two methods. One method should return the item at the current position in a list, and the other method should move the current position to the next item and return the last item. The code I have…
Yujia Sun
  • 1
  • 1
0
votes
0 answers

Why does the connection param of the closure need to be 'static bound?

The following code is invalid pub async fn async_execute_in_transcation>( connection: &mut T, ) -> Result<(), Error> { connection.transaction::<(), diesel::result::Error, _>(|conn| async { …
Hsu Jason
  • 21
  • 2
0
votes
2 answers

Return a value with longer lifetime in function

I need to return a Vector of &str from a function. To do that I return the value in a parameter that I have passed in a reference. Here is the initial code that I want to write: fn read_input_file(input_file: String, path_list: &mut Vec<&str>) { …
Jerome
  • 15
  • 4
0
votes
0 answers

Loop variable lifetime

Why does this example gives me ten times 10? namespace ConsoleApp { internal class Program { delegate void Printer(); static void Main(string[] args) { List printers = new List(); …
Aaras
  • 9
  • 3
0
votes
3 answers

Is it possible to write this program without using lifetime parameter?

Is it possible to write this program without using the lifetime parameter in pub fn anagrams_for<'a>(word: &'a str, possible_anagrams: &'a [&'a str]) -> Vec<&'a str> ? main.rs pub fn is_anagram(one_word: &str, other_word: &str) -> bool { …
user366312
  • 16,949
  • 65
  • 235
  • 452
0
votes
0 answers

Rust not allowing mutable borrow after immutable borrow should be dropped

I am basically tring to make a bash parser. I have a thing setup where a top level parser calls a parser for the next level down, for roughly three levels (script, pipeline, command). I am not getting any errors on the CommandParser bit, but on the…
0
votes
1 answer

Why do I get the "correct" output despite accessing an object that went ouf of scope?

The code below will print 10, but the t1 object (created in the function assign) no longer exists by the time the print happens, so is the ptr pointer pointing to unallocated memory space that still holds the value 10? #include class…
0
votes
1 answer

Passing a variable so a function recreates it in rust

I'm trying to insert multiple pieces into a hashmap as so fn main() { let mut pieces: HashMap<&str, Piece> = HashMap::new(); for column in b'a'..=b'h' { let column_char: char = column as char; let piece_position: String…
Taonga07
  • 36
  • 4
0
votes
0 answers

Using pyo3 and maturin for Python bindings. How to structure it?

For an existing library, what is the best (or at least good) way to structure the project when creating Python bindings via pyo3 / maturin ? So far I figure I can either just annotate all the structs and impls with #[pyclass] and #[pymethods]…
FISR
  • 13
  • 1
  • 5
0
votes
1 answer

How do lifetimes and variables captured by closures interact?

I'm currently learning Rust from the the book 'The Rust Programming Language' and seemingly I've encountered the following: fn main() { let mut s = String::from("Hello"); let mut add_suffix = || s.push_str(" world"); println!("{s}"); …
Kami SM
  • 29
  • 6
0
votes
1 answer

How to extend variable lifetime when borrowing

I'm preparing a script for PDB information extraction. Currently I cannot use info variable even once because of its short lifetime. I get 'borrowed value does not live long enough' error with the code below. I understand this happens because of…
mimak
  • 211
  • 2
  • 9
1 2 3
99
100