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

std::move and lifetime of temporary objects

Can someone explain the execution order of this code? struct Foo { ~Foo() { std::cout << "1"; } }; int main() { const Foo& bar = Foo(); const Foo& baz = std::move(Foo()); std::cout << "2"; } The following code prints…
Darryl Jordan
  • 305
  • 1
  • 6
19
votes
2 answers

Is the object returned from a function still created when it is not used?

Consider the following code. What happens when doStuff() is called but the return value is not used? Is SomeClass still created? Of course the creation itself can have important side effects, but so can copy-constructors and they are still omitted…
AdyAdy
  • 988
  • 6
  • 19
19
votes
1 answer

What is lifetime elision in very simple terms?

From the Rust documentation: Rust supports powerful local type inference in the bodies of functions, but it deliberately does not perform any reasoning about types for item signatures. However, for ergonomic reasons, a very restricted secondary…
davis
  • 341
  • 2
  • 10
19
votes
1 answer

Writing a generic function that takes an iterable container as parameter in Rust

I want to write a generic function that takes any immutably borrowed iterable container such as an array, Vec, BTreeSet, etc. Since this function is part of a trait that I am implementing, I am not able to change the signature of it, so it's not…
Zoidberg
  • 228
  • 2
  • 9
19
votes
2 answers

Temporary lifetime extension

Section 12.2.5 of the standard says: A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function…
Kolyunya
  • 5,973
  • 7
  • 46
  • 81
18
votes
1 answer

Strange lifetime error when iterating over a BTreeSet asynchronously

I want the async block in the following code to implement Send (Playground): use std::collections::BTreeSet; use std::future::ready; pub fn test(set: &BTreeSet) -> impl Send + '_ { async move { for _ in set { …
alephalpha
  • 560
  • 5
  • 13
18
votes
1 answer

When an array is created by a subexpression, what happens with the temporaries therein?

I was reading these two paragraphs of the FDIS (12.2p{4,5}): There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize…
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212
18
votes
3 answers

Prevent Android activity from being recreated on turning screen off

How to prevent an activity from being recreated on turning screen off? What I do Start Bejewels and go to the jewelry screen. Press power button shortly. The screen is turned off, but the device is not. Press power button again. What I see The…
17
votes
2 answers

what is the lifetime of javascript anonymous function?

If I write this in global scope: (function(){})(); is the anonymous function created when the statement is executed and destroyed immediately after the statement is executed? if I write this in a function: function foo() { var a=1; …
William
  • 761
  • 2
  • 10
  • 27
17
votes
2 answers

When returning the outcome of consuming a StdinLock, why was the borrow to stdin retained?

Given the following function: use std::io::{BufRead, stdin}; fn foo() -> usize { let stdin = stdin(); let stdinlock = stdin.lock(); stdinlock .lines() .count() } This fails to compile with the following error: error:…
E_net4
  • 27,810
  • 13
  • 101
  • 139
17
votes
6 answers

Borrow checker doesn't realize that `clear` drops reference to local variable

The following code reads space-delimited records from stdin, and writes comma-delimited records to stdout. Even with optimized builds it's rather slow (about twice as slow as using, say, awk). use std::io::BufRead; fn main() { let stdin =…
Clément
  • 12,299
  • 15
  • 75
  • 115
17
votes
4 answers

"constructing" a trivially-copyable object with memcpy

In C++, is this code correct? #include #include struct T // trivially copyable type { int x, y; }; int main() { void *buf = std::malloc( sizeof(T) ); if ( !buf ) return 0; T a{}; std::memcpy(buf, &a,…
M.M
  • 138,810
  • 21
  • 208
  • 365
16
votes
1 answer

c++ lifetieme extension with different parentheses

I'm trying to understand lifetime extension guarantees in C++. Can someone explain why the usage of different types of parentheses below yields differing results in terms of when the temporary object destructor is invoked? #include struct…
user3882729
  • 1,339
  • 8
  • 11
16
votes
1 answer

Reborrowing of mutable reference

When I wondered how a mutable reference could move into a method, all the questions began. let a = &mut x; a.somemethod(); // value of a should have moved a.anothermethod(); // but it works. I've googled a lot. (really a lot) And I've noticed that…
kwonryul
  • 481
  • 3
  • 10
16
votes
1 answer

How does the life-time of an LAContext instance in iOS 8 behave?

I am wondering how the lifetime of a LAContext instance from the LocalAuthentication framework looks like in iOS 8. In iOS 9 and later, there is the invalidate method to manually invalidate the current context. If I am not using that method, the…
Hans Knöchel
  • 11,422
  • 8
  • 28
  • 49