lifetime

Why can't I return an &str value generated from a String?

家住魔仙堡 提交于 2019-11-27 09:27:08
I'm having some trouble trying to grasp why I can't return an &str value generated from a String (goodness, when will as_str be ready?) and I'm doing something wrong. I get this idea because nothing that I do makes the value live long enough to use. I'm trying to implement error::Error for a custom struct: impl error::Error for LexicalError { fn description(&self) -> &str { let s = format!("{}", self); // s doesn't live long enough to do this, I've tried // cloning s and using that, but still the clone doesn't // live long enough. s.trim() } fn cause(&self) -> Option<&error::Error> { None } }

Why is the bound `T: 'a` required in order to store a reference `&'a T`?

回眸只為那壹抹淺笑 提交于 2019-11-27 09:12:04
Given this code: struct RefWrapper<'a, T> { r: &'a T, } ... the compiler complains: error: the parameter type T may not live long enough consider adding an explicit lifetime bound T: 'a so that the reference type &'a T does not outlive the data it points at. I've seen this error multiple times already and so far I just listened to the compiler and everything worked out fine. However, thinking more about it, I don't understand why I have to write T: 'a . As far as I understand, it is already impossible to get such a reference. Having &'a T implies that there is an object of type T that lives

Is it possible to have a struct which contains a reference to a value which has a shorter lifetime than the struct?

ぃ、小莉子 提交于 2019-11-27 08:50:15
问题 Here is a simplified version of what I want to archive: struct Foo<'a> { boo: Option<&'a mut String>, } fn main() { let mut foo = Foo { boo: None }; { let mut string = "Hello".to_string(); foo.boo = Some(&mut string); foo.boo.unwrap().push_str(", I am foo!"); foo.boo = None; } // string goes out of scope. foo does not reference string anymore } // foo goes out of scope This is obviously completely safe as foo.boo is None once string goes out of scope. Is there a way to tell this to the

Thread references require static lifetime?

大城市里の小女人 提交于 2019-11-27 08:37:26
问题 While it makes sense intuitively that references passed to spawned threads need to have static lifetimes, I'm unclear about what exactly is making the following code not compile: use std::sync::Arc; use std::sync::Mutex; struct M; fn do_something(m : Arc<Mutex<&M>>) { println!("Ha, do nothing!"); } fn main() { let a = M; { let c : Arc<Mutex<&M>> = Arc::new(Mutex::new(&a)); for i in 0..2 { let c_clone = c.clone(); ::std::thread::spawn(move || do_something(c_clone)); } } } Compiling this small

What should be the lifetime of an NHibernate session?

喜夏-厌秋 提交于 2019-11-27 07:12:36
I'm new to NHibernate, and have seen some issues when closing sessions prematurely. I've solved this temporarily by reusing sessions instead of opening a session per transaction. However, I was under the impression that opening sessions each time you need them was the recommended approach for session lifetime management. No? So; what is the recommended way of handling sessions? What should their lifetime be? One session pr transaction? One singleton session to handle everything? Or what? Edit: Note that my application architecture is a desktop application communicating with a server side

How do I bound a generic type with a trait that requires a lifetime parameter if I create the reference inside the function?

岁酱吖の 提交于 2019-11-27 07:03:38
问题 I want to implement a generic fibonacci function that works with any type implementing Zero , One , and AddAssign . I first implemented a version that works fine, but is specialized for num::BigUint (see on play.rust-lang.org). I than came up with the following generic implementation (see on play.rust-lang.org): extern crate num; use num::{One, Zero}; use std::mem::swap; use std::ops::AddAssign; fn fib<T: Zero + One + AddAssign<&T>>(n: usize) -> T { let mut f0 = Zero::zero(); let mut f1 = One

Does <'a, 'b: 'a> mean that the lifetime 'b must outlive the lifetime 'a?

六眼飞鱼酱① 提交于 2019-11-27 05:52:19
问题 I want to implement a builder similar to the debug builders defined by the standard library. They are defined using structures like the following: struct DebugFoo<'a, 'b: 'a> { fmt: &'a mut std::fmt::Formatter<'b> } Since I don't understand what the form <'a, 'b: 'a> means nor I can find it mentioned in the Rust book or the Rust reference (at least concerning lifetimes), I just tried to remove what I don't understand to see what happens: struct DebugFoo<'a, 'b> { fmt: &'a mut std::fmt:

Compiler asking for lifetime in struct when lifetime is given

风流意气都作罢 提交于 2019-11-27 04:56:42
问题 I'm trying to write the examples in the book "SFML Game Development" but I'm having a problem with the lifetimes for the struct that's supposed to represent the game world. The error is as follows: extern crate sfml; use self::sfml::window::*; use self::sfml::graphics::*; pub struct Game<'s> { mWindow: RenderWindow, mPlayer: &'s CircleShape, } Error message: error[E0106]: missing lifetime specifier --> src/game.rs:8:18 | 8 | mPlayer: &'s CircleShape, | ^^^^^^^^^^^ expected lifetime parameter

Type mismatches resolving a closure that takes arguments by reference

被刻印的时光 ゝ 提交于 2019-11-27 04:52:09
问题 I'm encountering a strange pair of errors while trying to compile my Rust code below. In searching for others with similar problems, I came across another question with the same combination of (seemingly opposing) errors, but couldn't generalize the solution from there to my problem. Basically, I seem to be missing a subtlety in Rust's ownership system. In trying to compile the (very pared down) code here: struct Point { x: f32, y: f32, } fn fold<S, T, F>(item: &[S], accum: T, f: F) -> T

Rust lifetime error expected concrete lifetime but found bound lifetime

拜拜、爱过 提交于 2019-11-27 04:07:23
问题 I am having an issue working with lifetime parameters for structs. I am not 100% sure how to describe the problem, but I created a trivial case that shows my compile time error. struct Ref; struct Container<'a> { r : &'a Ref } struct ContainerB<'a> { c : Container<'a> } trait ToC { fn to_c<'a>(&self, r : &'a Ref) -> Container<'a>; } impl<'a> ToC for ContainerB<'a> { fn to_c(&self, r : &'a Ref) -> Container<'a> { self.c } } The error I am getting with this is test.rs:16:3: 18:4 error: method