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
1
vote
1 answer

error: cannot infer an appropriate lifetime for autoref due to conflicting requirements [E0495]

First of all: I am fully aware of this post: Cannot infer appropriate lifetime for autoref in Iterator impl and that the problem is probably similar to mine. However, I can't get it working with the knowledge of this thread. The code: use…
robbepop
  • 119
  • 10
1
vote
2 answers

Is a local defined array valid outside its function where it is defined?

If we define an array inside SomeFunction it is valid in its scope. But where is it stored actually? Local variables are usually stored on the stack but I was asking myself if the whole array stored on the stack or just the pointer to the datafield,…
Frodo
  • 749
  • 11
  • 23
1
vote
1 answer

How to fix lifetime error due to RFC 1214

Consider the following code: pub fn use_r(xs: I, r: &R) { unimplemented!() } fn test<'a, R>(r: &'a mut R) { let a = |r: &'a mut R| { [(|| use_r(vec![0.].into_iter(), r))()] }; a(r); // a(r); } fn test2(r: &mut…
yong
  • 3,583
  • 16
  • 32
1
vote
0 answers

How can I manage the lifetime of my PHP sessions?

I am quite new to PHP, so I'm working on a simple project to practise. However, I can't manage to make the session management work properly. What I want is that when the browser is closed, the data (the current page the user is at) is saved for an…
rvvermeulen
  • 169
  • 8
1
vote
1 answer

Is boxing or explicit lifetimes the right solution when referencing a collection item in a loop?

In Rust (version 1.x) I want to use elements of a collection inside a loop such as the example below (which recors the characters it has seen and does something when it spots a repeated char) where the collection is defined inside the function and…
1
vote
2 answers

Name and lifetime of variables at compile time

From all the languages that I know (Java, C++), the name and the lifetime of a variable is always known at compile time. Is there any language for which the name and lifetime will be bound at run time? Furthermore, I'm playing with C++ smart…
Bobby
  • 496
  • 5
  • 18
1
vote
4 answers

Lifetime of const reference variable not extended

Binding a temporary to a const reference extends its lifetime; cf. GotW #88. Why does not this work on this snippet? Live here. #include #include struct A { A() : s("abc") {} const std::string& s; }; struct B { …
unagi
  • 428
  • 3
  • 15
1
vote
2 answers

Lifetime Errors using filter_map

I'm trying to use Iterator's filter_map function with a HashMap in Rust, but I can't get it to compile. Suppose I have a HashMap and a list of keys. For each key, if the map contains the key, I mutate the corresponding value in the map. For…
Iceberg
  • 376
  • 1
  • 12
1
vote
0 answers

Iterator with `&mut` items

I want to create an iterator for iterating through records in a huge file. for record in huge_file.iter() { println!("{}", record.read_field("name")); } Each record individually is too big to fit in memory, so the iterator can't return owned…
dflemstr
  • 25,947
  • 5
  • 70
  • 105
1
vote
1 answer

How can I lend a `str` to a thread without copying?

Given fn greet(peeps: &str) { println!("Hello, {}", peeps); } I can do: fn main() { let a = "World"; thread::spawn(move || greet(a)).join().unwrap(); } The compiler understands that the thread does not outlive the borrowed string, but…
Magnus Hoff
  • 21,529
  • 9
  • 63
  • 82
1
vote
0 answers

Returning an object and references to it from one function

I would like to test the following struct, where Bar and Baz are traits: struct Foo { bar: Box, baz: Box, } impl Foo { pub fn new(bar: Box, baz: Box) -> Foo { Foo { bar: bar, baz: baz } } } In my test for Foo, I…
Dino
  • 1,576
  • 12
  • 12
1
vote
1 answer

How can I have a lifetime dependency without a reference?

I'm wrapping a C library that has context and device objects. The context object needs to outlive the device object because the device keeps an internal reference to the context. To express this, I use a PhantomData field in the Device wrapper: use…
awelkie
  • 2,422
  • 1
  • 22
  • 32
1
vote
2 answers

Why do I get the error "cannot borrow x as mutable more than once"?

I'm implementing a parser in Rust. I have to update the index for the lookahead, but when I call self.get() after self.current() I get an error: cannot borrow *self as mutable more than once at a time It's confusing since I'm new to Rust.…
Akshay Deep Giri
  • 3,139
  • 5
  • 25
  • 33
1
vote
0 answers

session ended though lifetime didn't end in php

I'm using this code to set a login session from this answer issue with session lifetime $lifetime=604800; session_start(); setcookie(session_name(),session_id(),time()+$lifetime); checking the resources tab from inspect element the session cookie…
PHP User
  • 2,350
  • 6
  • 46
  • 87
1
vote
0 answers

Why does allocating temporary pointers seem to be causing stack overflow?

In the example below, when using GCC version 4.3.3 based target powerpc compiler, it seems every Add method call increases the used size of the stack. What I have known until today is that, after that Add method is called, that temporary variable…
Ali Burak Kulakli
  • 562
  • 1
  • 5
  • 16