Questions tagged [lifetime-scoping]

97 questions
4
votes
1 answer

"does not live long enough" error when calling `roots` from rust-xcb

I'm trying to use a Cairo surface in xcb-created window. I have a C example as well as Rust XCB and Cairo bindings. I'm almost finished, but this error remains a mystery to me. My code: fn find_visual<'a>(conn: &'a xcb::Connection, visual:…
dimcha
  • 193
  • 14
3
votes
1 answer

Cast requires that variable is borrowed for 'static

pub struct IterOverVecVec<'a> { m: &'a dyn IterTrait, } impl<'a> Iterator for IterOverVecVec<'a> { type Item = u16; fn next(&mut self) -> Option { Some(1) } } impl<'a> IterOverVecVec<'a> { fn new(m: &'a dyn…
TB_erryin
  • 33
  • 6
3
votes
1 answer

LifetimeScoping error in Web API self hosted app and Simple Injector

I read these (+ , + , + and +) pages, but I cannot figure out what should I do. I have this simple interface and concrete type: public interface IIdentifierGenerator { long Generate(Type type); long Generate(TType type); } public…
2
votes
0 answers

Rust ref mut vs .as_mut() when using inside while let Some

I'm having trouble understanding the difference in the two examples are and why the second doesn't work? I can't assign tho list.next in the second because it's borrowed as mutably. struct List { value: T, next:…
Domasch
  • 21
  • 2
2
votes
2 answers

Lifetime specification for closure return type

I have the following snippet of code (lifetimes attempts elided): pub struct NamedArgument(pub(in crate) &'static str, pub(in crate) T); pub struct LoggedArgument(pub(in crate) &'static str, pub(in crate) T); impl NamedArgument { …
Folling
  • 415
  • 3
  • 12
2
votes
1 answer

Converting a struct with ownership to use lifetimes and borrowed copies: no more Default?

I have a library that I'm writing that's entirely based around ownership. Mostly as an academic exercise, I'm looking at migrating it to use lifetimes. Given something like this, #[derive(Default)] struct Foo { test: i32 } #[derive(Default)] struct…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
2
votes
1 answer

Why do Rust lifetimes matter when I move values into a spawned Tokio task?

I'm trying to create a struct that will manage a Tokio task with one tokio::sync::mpsc::Sender that sends input to the task, one tokio::sync::mpsc::Receiver that receives output from the task, and a handle that I can join at the end. use…
John Stanford
  • 993
  • 1
  • 7
  • 18
2
votes
1 answer

Rust lifetime scoping in structs

So, I'm working on porting a string tokenizer that I wrote in Python over to Rust, and I've run into an issue I can't seem to get past with lifetimes and structs. So, the process is basically: Get an array of files Convert each file to a…
dougalg
  • 529
  • 4
  • 14
2
votes
2 answers

What is the point of an explicit lifetime for a method that doesn't take any arguments?

On page 295 of Programming Rust you can find the following: Fortunately, the standard library includes the blanket implementation: impl<'a, T, U> AsRef for &'a T where T: AsRef, T: ?Sized, U: ?Sized, { fn as_ref(&self) -> &U…
Evan Carroll
  • 78,363
  • 46
  • 261
  • 468
2
votes
2 answers

Lifetimes in generic trait implementation

I want to make a trait similar to the below structure (my particular use case is a bit more complex but this captures the issue and error I'm getting). The issue I have is which lifetimes in the last impl. I think I need to squeeze them into the…
Clinton
  • 22,361
  • 15
  • 67
  • 163
2
votes
0 answers

Accessing different fields of the same struct simultaneously and mutably

I'm trying to write a card game in Rust but ran into some trouble with the borrow checker. My main struct is the Game, which looks like this struct Game { players: HashMap, deck: Vec, } Player is something like struct…
red_trumpet
  • 601
  • 1
  • 6
  • 15
2
votes
1 answer

Mutable reference lives long enough when function is called directly, but doesn't live long enough when called through an intermediate function

For the following Rust code: fn facing_of_mut<'a>(domain: &'a mut Domain, entity: Entity) -> Option<&'a mut Direction> { component_of_mut(&mut domain, entity) } ...the compiler outputs: error: `domain` does not live long enough -->…
Flaise
  • 571
  • 3
  • 11
2
votes
1 answer

Is it possible to add registrations to an Autofac LifetimeScope after it was created?

I know that you can add registrations to a LifetimeScope when it is created like this: using(var scope = container.BeginLifetimeScope(builder => { builder.RegisterType().As(); builder.RegisterModule(); …
dmorganb
  • 1,408
  • 16
  • 26
2
votes
2 answers

C++ Destroying Variables via Scoping

Is it safe or acceptable practice to create 'temporary' objects in C++ with an empty scope (such as the following), in order to ensure they are immediately destroyed? { SingularPurpose singular(&ptr_to_something); }
Bryn McKerracher
  • 683
  • 6
  • 21
2
votes
1 answer

Scoping of a for loop in Javascript

Consider the following code: for (var x = 0; x < 10; x++) { /* do awesome stuff. 10 times! */ } console.log(x); x is still printed in the console. This gives me warnings in JSHint, because a couple of lines further I do another loop, redeclaring…
Anemoia
  • 7,928
  • 7
  • 46
  • 71