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
34
votes
2 answers

How can this instance seemingly outlive its own parameter lifetime?

Before I stumbled upon the code below, I was convinced that a lifetime in a type's lifetime parameter would always outlive its own instances. In other words, given a foo: Foo<'a>, then 'a would always outlive foo. Then I was introduced to this…
E_net4
  • 27,810
  • 13
  • 101
  • 139
34
votes
2 answers

What does the first explicit lifetime specifier on an impl mean?

There are three different lifetime specifiers on an impl: impl<'a> Type<'a> { fn my_function(&self) -> &'a u32 { self.x } } Type<'a> states that there is a lifetime in this impl declaration. The one on the return type -> &'a u32…
xetra11
  • 7,671
  • 14
  • 84
  • 159
32
votes
5 answers

Why don't static arrays need to be freed?

I am wondering why static arrays don't need to be freed? I know that when creating a dynamic array e.g. int *p; p = malloc(10*sizeof(int)); we have to free the allocated memory by using: free(p); And for a static array in a function, the static…
BL_
  • 821
  • 1
  • 17
  • 23
32
votes
1 answer

Struggling with the subtyping relation of lifetimes in Rust

I feel dumb for having browsed the marker section of the Rust documentation and the Wikipedia articles about subtyping and variance multiple times without it improving my understanding of the lifetimes subtyping relation. I think I'm just used to…
sellibitze
  • 27,611
  • 3
  • 75
  • 95
30
votes
2 answers

What does "Box" mean in rust?

What does Box mean in rust? I stumbled upon this syntax while reading advanced types chapter. Send is a trait but what does it mean to + a lifetime to a trait ('static in this case) in type parametrization ? Also what is Fn()…
soupybionics
  • 4,200
  • 6
  • 31
  • 43
30
votes
2 answers

Why is the bound `T: 'a` required in order to store a reference `&'a T`?

Given this code: struct RefWrapper<'a, T> { r: &'a T, } ... the compiler complains: error: the parameter type T may not live long enough consider adding an explicit lifetime bound T: 'a so that the reference type &'a T does not outlive the…
Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
27
votes
7 answers

How to set lifetime of session

How to set session lifetime in PHP? I Want to set it to forever as long as the request is exist. The request is AJAX. My PHP code that handle AJAX request is: // AJAX.php
Hensembryan
  • 1,067
  • 3
  • 14
  • 31
27
votes
3 answers

Why can't I use a key function that returns a reference when sorting a vector with sort_by_key?

I'm trying to sort a Vec using a key function that returns references to the strings in the vector. A contrived example is to use the identity function as key function (which of course is useless, but it's the minimal example to reproduce…
Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
27
votes
3 answers

How to declare a higher-ranked lifetime for a closure argument?

I would like to declare a lifetime for a closure in Rust, but I can't find a way to add a lifetime declaration. use std::str::SplitWhitespace; pub struct ParserError { pub message: String, } fn missing_token(line_no: usize) -> ParserError { …
Peter Smit
  • 27,696
  • 33
  • 111
  • 170
26
votes
2 answers

How to fix lifetime error when function returns a serde Deserialize type?

I'm using serde and serde_json 1.0 to decode data from a base64 string: fn from_base64_str(string: &str) -> T { let slice = decode_config(string, URL_SAFE).unwrap(); serde_json::from_slice(&slice).unwrap() } When I…
realli
  • 990
  • 6
  • 18
26
votes
2 answers

Why does linking lifetimes matter only with mutable references?

A few days ago, there was a question where someone had a problem with linked lifetimes of a mutable reference to a type which contained borrowed data itself. The problem was supplying a reference to the type with a borrow of the same lifetime as the…
jtepe
  • 3,242
  • 2
  • 24
  • 31
26
votes
2 answers

Does <'a, 'b: 'a> mean that the lifetime 'b must outlive the lifetime 'a?

I want to implement a builder similar to the debug builders defined by the standard library. They are defined using structures like the following: struct DebugFoo<'a, 'b: 'a> { fmt: &'a mut std::fmt::Formatter<'b> } Since I don't understand…
toro2k
  • 19,020
  • 7
  • 64
  • 71
25
votes
2 answers

Connection Timeout and Connection Lifetime

What is the advantage and disadvantage of connection timeout=0? And what is the use of Connection Lifetime=0? e.g (Database=TestDB; port=3306; Uid=usernameID; Pwd=myPassword; Server=192.168.10.1; Pooling=false; Connection Lifetime=0; …
Mark
  • 421
  • 5
  • 11
  • 16
24
votes
4 answers

Explicit call to destructor

I stumbled upon the following code snippet: #include #include using namespace std; class First { string *s; public: First() { s = new string("Text");} ~First() { delete s;} void Print(){ cout<<*s;} }; int…
24
votes
3 answers

Why does the lifetime name appear as part of the function type?

I believe that this function declaration tells Rust that the lifetime of the function's output is the same as the lifetime of it's s parameter: fn substr<'a>(s: &'a str, until: u32) -> &'a str; ^^^^ It seems to me that the compiler only…
Wayne Conrad
  • 103,207
  • 26
  • 155
  • 191