Questions tagged [lifetime-scoping]
97 questions
148
votes
1 answer
What are non-lexical lifetimes?
Rust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust's support of this feature has improved a lot and is considered complete.
My question is: what exactly is a…

Stargateur
- 24,473
- 8
- 65
- 91
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
19
votes
1 answer
Mediatr Scope problems
I am using Mediatr to handle messages from a queue. I can get a simple example to work. However I have run into problems when I try to inject an object into my handler
public class MessageCommandHandler : IRequestHandler
{
…

rideintothesun
- 1,628
- 2
- 12
- 29
13
votes
1 answer
When do I need to specify explicit lifetimes in Rust?
If I have the two functions
// implicit
fn foo(x: &i32) {
}
// explicit
fn bar<'a>(x: &'a i32) {
}
When would foo return an error and bar be the correct function header? I'm confused as to why I would explicitly declare a lifetime:
The 'a reads…

Syntactic Fructose
- 18,936
- 23
- 91
- 177
13
votes
2 answers
How do I use static lifetimes with threads?
I'm currently struggling with lifetimes in Rust (1.0), especially when it comes to passing structs via channels.
How would I get this simple example to compile:
use std::sync::mpsc::{Receiver, Sender};
use std::sync::mpsc;
use…

marekventur
- 1,875
- 1
- 16
- 22
9
votes
1 answer
Can multiple Autofac lifetime scopes be specified on a registration?
I'm using the Autofac IoC container with the MVC4 add-on which provides the InstancePerHttpRequest lifetime scope. However within my project I have the web, web-api and background worker threads. In the following example I assume the…

chriskopec
- 605
- 1
- 7
- 8
7
votes
1 answer
DocumentDB client lifetime
To access DocumentDB/CosmosDB I'm using package Microsoft.Azure.DocumentDB.Core(v1.3.2). I have noticed when I create and initialise DocumentClient class:
var documentClient = new DocumentClient(new Uri(endpointUrl), primaryKey);
await…

trailmax
- 34,305
- 22
- 140
- 234
6
votes
1 answer
Passing two objects, where one holds a reference to another, into a thread
I have two objects where the second one requires the fist one to outlive it because it holds a reference to the first one. I need to move both of them into a thread, but the compiler is complaining that the first one doesn't live long enough. Here…

Peter Jankuliak
- 3,464
- 1
- 29
- 40
6
votes
3 answers
Instance per matching lifetime scope, with default?
I'd like to have an instance per matching lifetime scoped registration in Autofac, but occasionally need to request an instance from a global container (where there is no matching lifetime scope). In scenarios where no matching lifetime scope…

David Pfeffer
- 38,869
- 30
- 127
- 202
5
votes
1 answer
Why doesn't an async function with two non-static references and a static reference compile?
An async function with one static reference compiles:
pub async fn test_0(_a: &'static str) {
}
An async function with a non-static and a static reference compiles:
pub async fn test_1<'a>(_a: &'a str, _b: &'static str) {
}
An async function…

Jeff Cutsinger
- 53
- 4
5
votes
5 answers
Variable lifetime
What happends to variable when line of execution goes outside of code block?
For example:
1 public void myMethod()
2 {
3 int number;
4 number = 5;
5 }
so, we declare and set variable. When it goes outside of code block (line 5) what…

ExpertLoser
- 257
- 4
- 14
4
votes
1 answer
Why is the lifetime of my returned impl Trait restrained to the lifetime of its input?
In trying to work out why some code of mine will not compile, i have created the following minimal test.
I am trying to write a function that receives something like an &Vec and returns somthing that can be converted into an Iterator over…

cds84
- 343
- 1
- 14
4
votes
1 answer
"one type is more general than the other" error in Rust while types are identical
I have the following code
use std::future::Future;
fn main() {
handle(Test::my_func);
}
fn handle(fun: for<'r> fn(&'r mut Test) -> Fut) -> bool
where
Fut: Future

gurghet
- 7,591
- 4
- 36
- 63
4
votes
1 answer
Restricting object lifetimes in Rust
I'm wrapping a C library, and it has a standard sort of context object:
library_context* context = library_create_context();
And then using that you can create more objects:
library_object* object = library_create_object(context);
And destroy them…

Timmmm
- 88,195
- 71
- 364
- 509
4
votes
2 answers
Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?
In implementing a version of a LazyList (an immutable lazily-computed memoized singly-linked list, much as Haskell lists), I have run into a problem of implementing IntoIterator in that the code does not drop the reference when I think it should. …

GordonBGood
- 3,467
- 1
- 37
- 45