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

Using serde_json to deserialize from Vec

We have a vector Vec that we want to deserialize to a type T. The minumum reproducing example we could come up with is fn wrapper<'de, T>(vec: Vec) -> Result where T: serde::Deserialize<'de>, { …
0
votes
1 answer

Borrowed data escapes outside of associated function

I'm new to Rust, and trying to learn the language by creating something around it. Facing this problem that the self is valid inside this function but the closure is async so it may be executed after the lifetime of this function and that closure…
bradley101
  • 723
  • 6
  • 19
0
votes
1 answer

Rust lifetime annotation in tree type

I'm new in rust community I'm writing a B tree implementation and have problems with refs #[allow(dead_code)] struct Node<'a, T: PartialOrd + Clone> { leaf: bool, count: usize, keys: Vec, children: Vec<&'a mut Node<'a,…
v8tenko
  • 21
  • 4
0
votes
1 answer

Lifetime problem when visitor recursively traversal AST

I have a parser toy that wants to transform AST value like SCSS nested rules, I got a problem is that when I visit every node, I would transform AST node's value computed by parent value and ancestor node value. So I use a context property of…
steven-lie
  • 75
  • 2
  • 7
0
votes
2 answers

How do I fix this lifetime issue. Trying to filter a device stream based on a its name (which is async retrieved)

I'm working on this function: use std::{sync::Arc, collections::HashSet}; use futures::{stream::BoxStream, StreamExt,TryStreamExt}; use brightness::{Brightness, BrightnessDevice}; fn get_devices<'a>(device_names: Arc>) ->…
Typhaon
  • 828
  • 8
  • 27
0
votes
1 answer

Can I save a closure along the captured environment to call it later?

I've came across this case where I would like to create a struct, that can be modified by a closure that is owned by the struct. I have never been too deep into closures and capturing environement, nor lifetimes, but I'm wondering why the borrow…
0
votes
0 answers

Matlab code for lifetime optimization in wireless body area networks

I wrote a matlab code for lifetime optimization using a cost function for relay selection for data forwarding, the residual energy in each node is respresented bynormal distribution, the result which is the relation between lifetime(no.of…
0
votes
3 answers

Why `std::mem::forget` cannot be used for creating static references?

pub trait Forget { fn forget_me_as_ptr(self) -> *const Self where Self: Sized, { let p = addr_of!(self); forget(self); p } fn forget_me_as_mut_ptr(self) -> *mut Self where Self:…
Akash R
  • 55
  • 6
0
votes
0 answers

Ownership and creating an AppState for an Axum server using the `whisper_rs` crate

I'm trying to create a whisper-rs server using Axum. In this server I'd like to only have to create the whisper state once at server startup. Hence, I created an AppState struct that could then be passed around using an…
0
votes
1 answer

Why does conditionally returning an immutable reference in a loop inhibit creating a mutable borrowing in the loop after that reference dropped?

fn foo(var: &mut String) -> &String { let mut counter = 1; let result = loop { { let var_ref = &*var; counter += 1; if counter > 10 { break var_ref; } } …
E221_
  • 1
  • 1
0
votes
1 answer

&str with lifetime for "After Deserialization"

I'm beating my head on lifetimes and &str. I have a bunch of strings in my program that aren't actually static, but come close: they're loaded from a json at initialization, and should remain in scope for the entire length of the program. I have a…
Edward Peters
  • 3,623
  • 2
  • 16
  • 39
0
votes
0 answers

Trait implementation with tighter requirements for closure parameter lifetime

I have this Parameter trait that I use to wrap access to resources which are acquired based on the type of the Parameter implementation. The closure on the call method is supposed to take a reference to the acquired resource but I cannot figure out…
Facundo Villa
  • 111
  • 2
  • 12
0
votes
1 answer

mismatched types : one type is more general than the other, using constrained Higher-Rank Trait Bounds

This is a follow up of "dropped here while still borrowed" when making lifetime explicits but can be looked at independently. Now that @jthulhu made me discover the Higher-Rank Trait Bounds that fixed my previous error, I want to go a step further…
0
votes
2 answers

How to call an async function with a non-static reference in `poll` in rust?

I've encountered an obstacle when trying to implement Future. I need to poll another async function in my poll function which only takes in &str. It looks something like this: async fn make_query(query: &str) -> i32 { // Magic happens …
Einliterflasche
  • 473
  • 6
  • 18
0
votes
1 answer

"dropped here while still borrowed" when making lifetime explicits

I am trying to improve my understanding of rust borrow checker by making implicit lifetimes explicit. It actually came from a bigger problem for work, but I boiled it down to this (so far). Let's take this code as an example : struct…
1 2 3
99
100