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

With Ninject, how do you specify different lifetimes depending on the binding?

In our MVC application we predominantly use Ninject to inject dependencies into controllers. As such, our default lifetime scope is InRequestScope(). We have now added an IHttpModule that uses common dependencies as the controllers (i.e.,…
DeveloperRob
  • 155
  • 1
  • 9
1
vote
1 answer

Storage duration when calling constructor from another function

I have a struct that looks like this: struct matrix { size_t nrow; size_t ncol; double *data; }; and a corresponding constructor: struct matrix *matrix_create(const size_t nrow, const size_t ncol) { struct matrix *m; m =…
lord.garbage
  • 5,884
  • 5
  • 36
  • 55
1
vote
1 answer

Argument lifetime of an asynchronous function call

Herb Sutter in GotW #91 Solution: Smart Pointer Parameters states " Thanks to structured lifetimes, the called function’s lifetime is a strict subset of the calling function’s call expression." Does this apply to asynchronous function calls? void…
StephenD
  • 231
  • 1
  • 10
1
vote
2 answers

Why is this trait/implementation incompatible - bound lifetime vs concrete lifetime

I'm struggling with this error rustc gives me: error: method `create_shader_explicit` has an incompatible type for trait: expected bound lifetime parameter 'a, found concrete lifetime My trait declaration is pretty much this: pub trait…
neon64
  • 1,486
  • 2
  • 12
  • 13
1
vote
1 answer

Trait type and lifetime issues

I'm trying to write an Iron plugin middleware, but I'm running into an issue when trying to define a typemap key: The minimal example with simple type works without issues: pub struct Database; impl Key for Database { type Value = isize; } But…
viraptor
  • 33,322
  • 10
  • 107
  • 191
1
vote
1 answer

What is the lifetime of an object immediately passed as a parameter?

I had some code that looked something like the following: struct fooclass { char data[32]; } fooclass makefoo() { fooclass res; // for example: memset(res.data, 0, sizeof(res.data)); res.data[0] = 43; return res; } struct…
Claudiu
  • 224,032
  • 165
  • 485
  • 680
1
vote
0 answers

List of shared pointers

I have a shared pointer to the result of some calculation as follows: std::list> ResultList; for (int i= 0; i < upperBound; i++) { DoCalculation(); std::shared_ptr Res; Res =…
QuestionMark
  • 412
  • 1
  • 4
  • 16
1
vote
1 answer

Conflicting lifetime requirement for iterator returned from function

This may be a duplicate. I don't know. I couldn't understand the other answers well enough to know that. :) Rust version: rustc 1.0.0-nightly (b47aebe3f 2015-02-26) (built 2015-02-27) Basically, I'm passing a bool to this function that's supposed to…
user1949917
1
vote
1 answer

Lifetime for passed-in function that is then executed in a thread

I'm trying to build a simple pipeline-like functionality that executes each stage of the pipeline is separate threads and glues them all together with channel passing. Pipe::source(buffer) .pipe(|input, output| {...}) .pipe(|input, output|…
mrak
  • 494
  • 5
  • 10
1
vote
2 answers

Caught between a lifetime and an FFI place

I am caught between two different issues/bugs, and can't come up with a decent solution. Any help would be greatly appreciated Context, FFI, and calling a lot of C functions, and wrapping C types in rust structs. The first problem is ICE: this path…
Tupshin Harper
  • 1,267
  • 9
  • 12
1
vote
1 answer

Adding lifetime constraints to non-reference types

I am trying to figure out how to apply Rust lifetimes to add some compile-time enforcement to Erlang NIF modules. NIF modules are shared libraries normally written in C that provide extensions. A simplified prototype of the callback you would write…
goertzenator
  • 1,960
  • 18
  • 28
1
vote
2 answers

Magento - How do I initialize a new Quote for a given Checkout/Session?

I'm setting up a custom controller to extend Mage_Core_Controller_Front_Action. If a user adds an item to the cart, I want to check the quote lifetime. If it is old enough, then the user should be given a new quote. Nothing needs to be done for…
0dyss3us
  • 91
  • 1
  • 6
1
vote
1 answer

Extending borrow lifetimes in rust

I'm trying to parse a series to tokentrees, but when I try to implement my parsing trait I get an error related to reference lifetimes. I thought creating a boxed version would move around any issues with reference counts or lifetimes. The code is…
ragingSloth
  • 1,094
  • 8
  • 22
1
vote
1 answer

Wrong number of lifetime parameters when encoding a generic type

I'm trying to write a generic function to send some data, wrapped into a parent-struct. The data should then be encoded as JSON and sent with a socket. extern crate serialize; use serialize::json; use serialize::serialize::{Encodable, Encoder}; use…
olee
  • 735
  • 5
  • 14
1
vote
1 answer

Aliasing a closure type in Rust, but compiler asks for lifetime specifier

I'm a completely newbie to Rust or even to these languages that touches bare metal. I was going to alias a closure type that takes a few pointers as arguments, and returns a String. With the newest syntax (if I understood correctly) I wrote: pub…
Ning Sun
  • 2,145
  • 1
  • 19
  • 24