lifetime

Passing two objects, where one holds a reference to another, into a thread

自闭症网瘾萝莉.ら 提交于 2021-02-20 11:51:10
问题 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 is the code: use std::thread; trait Facade: Sync { fn add(&self) -> u32; } struct RoutingNode<'a> { facade: &'a (Facade + 'a), } impl<'a> RoutingNode<'a> { fn new(facade: &'a Facade) -> RoutingNode<'a> { RoutingNode { facade: facade } } } fn main()

What does a scoped lifetime in rust actually mean?

断了今生、忘了曾经 提交于 2021-02-18 02:25:57
问题 So, in: fn v1<'a> (a:~[&'a str]) -> ~[&'a str] { return a; } #[test] fn test_can_create_struct() { let x = v1("Hello World".split(' ').collect()); } I know, I've read http://static.rust-lang.org/doc/master/guide-lifetimes.html#named-lifetimes, but I don't understand what this code actually does . The function is basically parametrized like a generic fn but with a lifetime, is what I've seen said on the IRC channel, but lets imagine that is the case, and we have a L, which is some specific

How to handle “cannot infer an appropriate lifetime for autoref due to conflicting requirements”

半城伤御伤魂 提交于 2021-02-11 13:38:58
问题 I have this method from ExchangeInfo struct that returns a RwLockReadGuardRef from owning_ref crate: pub fn get_pair<'a, 'me: 'a>( &'me self, name: &str, ) -> RwLockReadGuardRef<'a, TradePairHashMap, Arc<RwLock<TradePair>>> { RwLockReadGuardRef::new(self.pairs.read().unwrap()).map(|pairs| pairs.get(name).unwrap()) } I'm trying to call it here: pub async fn get_pair<'a>( name: &str, exchange_info: &'a ExchangeInfo, retrieval: &dyn ExchangeInfoRetrieval, refresh: bool, ) -> Result<OwningRef<&'a

Why can't I store a value and a reference to that value in the same struct?

我们两清 提交于 2021-02-11 13:30:04
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<'a>(Thing, &'a u32); fn make_combined<'a>() -> Combined<'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<'a>(Thing, &'a Thing); fn make_combined<'a>() -> Combined<'a> { let thing = Thing::new();

Taking closures with explicit lifetimes as arguments in Rust

六眼飞鱼酱① 提交于 2021-02-10 19:56:26
问题 The closures section of Rust documentation has this example: fn call_with_ref<'a, F>(some_closure: F) -> i32 where F: Fn(&'a i32) -> i32 { let value = 0; some_closure(&value) } This doesn't compile because, as the docs put it: When a function has an explicit lifetime parameter, that lifetime must be at least as long as the entire call to that function. The borrow checker will complain that value doesn't live long enough, because it is only in scope after its declaration inside the function

What are non-lexical lifetimes?

不羁岁月 提交于 2021-02-10 16:42:34
问题 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 non-lexical lifetime? 回答1: It's easiest to understand what non-lexical lifetimes are by understanding what lexical lifetimes are. In versions of Rust before non-lexical lifetimes are present, this code will fail: fn main() { let mut scores = vec![1, 2

Why is Vec<&str> missing lifetime specifier here? [duplicate]

笑着哭i 提交于 2021-02-08 12:03:32
问题 This question already has an answer here : What does “missing lifetime specifier” mean when storing a &str in a structure? (1 answer) Closed 1 year ago . This code compiles: struct IntDisplayable(Vec<u8>); impl fmt::Display for IntDisplayable { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { for v in &self.0 { write!(f, "\n{}", v)?; } Ok(()) } } fn main() { let vec: Vec<u8> = vec![1,2,3,4,5]; let vec_Foo = IntDisplayable(vec); println!("{}",vec_Foo); } whilst this code doesn't: struct

Lifetime issues with a closure argument in Rust

佐手、 提交于 2021-02-08 06:53:21
问题 I'm getting an error when trying to use a closure that does exactly the same as the print function below (in ln.9) The error is the usual borrowed value does not live long enough . I've tried to replicate this in the playground but I can't. I'm certain that this is mainly because I don't really understand what's going on here so any help would be really appreciated. What I can't understand is what is the difference between calling the print function and calling the check closure. They have

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

你。 提交于 2021-02-07 08:12:36
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —

Why does the borrow checker disallow a second mutable borrow even if the first one is already out of scope?

ⅰ亾dé卋堺 提交于 2021-02-07 08:11:38
问题 Background I know that the borrow checker disallows more than one mutable borrows. For example, the code below is invalid: fn main() { let mut x = 42; let a = &mut x; let b = &mut x; println!("{} {}", a, b); } However, if the first borrow is dropped due to out of scope, the second borrow is valid: fn main() { let mut x = 1; { let a = &mut x; println!("{}", a); } let b = &mut x; println!("{}", b); } Because of non-lexical lifetimes (NLL), the first borrow doesn't even have to be out of scope —