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

lifetime not long enough rust

I want to open a file, replace some characters, and make some splits. Then I want to return the list of strings. however I get error: broken does not live long enough. My code works when it is in main, so it is only an issue with lifetimes. fn…
ragingSloth
  • 1,094
  • 8
  • 22
1
vote
1 answer

Borrow pointer of a value which will be moved in a struct

Let's say a Manager holds references to a Designer and a Programmer, but the Programmer also has to hold a reference to a Designer, in order to ping them whenever they want. The problem is, by creating both the Designer and the Programmer in…
razielgn
  • 46
  • 4
1
vote
1 answer

Lifetime of javascript variables in Cordova

I am confused with the notion of javascript variable lifetime in the context of Cordova applications. When is it ok to assume my javascript variables are defined when an app is running on a mobile device? Is it only when the app is open? Or do they…
Louis
  • 185
  • 9
1
vote
2 answers

How do I state that I want a struct which contains a reference to something which implements a trait?

Editor's note: The code presented in the question compiles as-is in Rust 1.0. I've tried: trait Inner {} struct Outer<'a> { inner: &'a Inner, } but the compiler complains: Compiling tst v0.1.0…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
1
vote
1 answer

Correctly setting lifetimes and mutability expectations in Rust

I'm rather new to Rust and have put together a little experiment that blows my understanding of annotations entirely out of the water. This is compiled with rust-0.13.0-nightly and there's a playpen version of the code here. The meat of the program…
troutwine
  • 3,721
  • 3
  • 28
  • 62
1
vote
2 answers

Create Entity context in class constrctor? or create it whenever need to use it?

I'm working on a project somebody else developed. In the project, it has public class HomeController : Controller { public HomeController() { _EntitiesContext = new EntitiesContext(); _UsersContext = new UsersContext(); } …
urlreader
  • 6,319
  • 7
  • 57
  • 91
1
vote
1 answer

Using structs in fields

I'm trying to understand the difference between the case where structs contain simple types vs. when they contain other structs. All guides/examples/... seem to use only basic types as fields and this works: struct Something { some: i32, …
viraptor
  • 33,322
  • 10
  • 107
  • 191
1
vote
1 answer

Passing a Vec into a new task

Im attempting to pass a Vector of a custom struct into a function that is executed in a new task. I've implemented the Clone trait, which I thought was needed for this, but apparently the vector I want to pass in needs to implement 'static+Send in…
nathansizemore
  • 3,028
  • 7
  • 39
  • 63
1
vote
2 answers

Index and IndexMut implementations to return borrowed vectors

I've been working on a multi-dimensional array library, toying around with different interfaces, and ran into an issue I can't seem to solve. This may be a simple misunderstanding of lifetimes, but I've tried just about every solution I can think…
Tom Scogland
  • 937
  • 5
  • 12
1
vote
1 answer

undefined reference during linkage for a static pointer class member in C++

Sorry if this is a duplicate. Here's one piece of example code that I cannot understand about static pointer class member. #include class MyField { public: MyField() { std::cout << "ctr for MyField\n"; } ~MyField() { std::cout <<…
Hongxu Chen
  • 5,240
  • 2
  • 45
  • 85
1
vote
2 answers

How does mutability affect the lifetime of `self` in the following method implementations?

In the following case, why does the implementation of get_foos_mut for B give a lifetime error while the implementation of get_foos works fine? trait Foo { fn get_foos<'a>(&'a self) -> Vec<&'a Foo> { Vec::new() } fn get_foos_mut<'a>(&'a mut…
mindTree
  • 916
  • 9
  • 18
1
vote
1 answer

Why does my saved D3 selection have no effect in some cases?

I'm confused about how to save a D3 selection for later use. In the code below, I have a "global" variable for my axes, to which I save them when they are first created. Later, I'm able to use this variable for certain things (here, setting some…
orome
  • 45,163
  • 57
  • 202
  • 418
1
vote
1 answer

Lifetime of an Imgur image

Well, I know that if an image is uploaded to Imgur service and is not seen within 6 months, it will be deleted. Now my question is: Is it considered as seen when I load the image from its direct URL (e.g: https://i.stack.imgur.com/r4stF.jpg) ? (note…
Eduardo
  • 218
  • 4
  • 15
1
vote
1 answer

Cassandra Session Lifetime

I am currently learning the basics of the Cassandra. I am using the Datastax java-driver to experiment with. Now I am currently working out the session structure. What I am wondering about is how to handle session lifetime. On the following…
Thizzer
  • 16,153
  • 28
  • 98
  • 139
1
vote
0 answers

How to efficiently determine cluster lifetimes in MATLAB?

I have a dataset on which on MATLAB/StatisticsToolbox I used linkage, (method=ward), to obtain a separation through hierarchical clustering. I want to separate those results such that the number of clusters is the one corresponding to the maximum…