lifetime

Borrowed value does not live long enough when creating a Vec

那年仲夏 提交于 2020-01-01 12:15:24
问题 Editor's note: This question was asked before Rust 1.0. Since then, many functions and types have changed, as have certain language semantics. The code in the question is no longer valid, but the ideas expressed in the answers may be. I'm trying to list the files in a directory and copy the filename to my own Vec . I've tried several solutions, but it always ends up with a problem of not being able to create long enough living variables. I don't understand my mistake. fn getList(action_dir

When an array is created by a subexpression, what happens with the temporaries therein?

元气小坏坏 提交于 2020-01-01 07:37:24
问题 I was reading these two paragraphs of the FDIS (12.2p{4,5}): There are two contexts in which temporaries are destroyed at a different point than the end of the full-expression. The first context is when a default constructor is called to initialize an element of an array. If the constructor has one or more default arguments, the destruction of every temporary created in a default argument is sequenced before the construction of the next array element, if any. and The second context is when a

Higher Ranked Trait Bound and boxed closures lifetime issue

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-01 02:49:12
问题 I am trying to write a function returning a boxed closure that can work on references to types with any lifetime. When writing a specific instance, everything works fine. But when writing a generic version, I run into lifetime problems. struct Parameter<'a> { s: &'a str, } fn main() { let closure = generate_closure_gen(); let string = String::from("Hello World!"); let parameter = Parameter { s: &string }; // Error: string does not live long enough closure(&parameter); } // This one works fine

Why does the lifetime name appear as part of the function type?

回眸只為那壹抹淺笑 提交于 2019-12-31 09:13:12
问题 I believe that this function declaration tells Rust that the lifetime of the function's output is the same as the lifetime of it's s parameter: fn substr<'a>(s: &'a str, until: u32) -> &'a str; ^^^^ It seems to me that the compiler only needs to know this(1): fn substr(s: &'a str, until: u32) -> &'a str; What does the annotation <'a> after the function name mean? Why does the compiler need it, and what does it do with it? (1): I know it needs to know even less, due to lifetime elision. But

How to fix: expected concrete lifetime, but found bound lifetime parameter

喜欢而已 提交于 2019-12-31 07:18:18
问题 I'm currently pulling my hear out over this one. I tried to shrink it down to a minimal reproducible example. struct Request; struct ResponseWriter<'a> { dummy: &'a () } #[deriving(Clone)] pub struct RouteStore{ pub routes: Vec<Route>, } #[deriving(Clone)] struct Route { path: String, handler: fn(request: &Request, response: &mut ResponseWriter) } impl RouteStore { pub fn new () -> RouteStore { RouteStore { routes: Vec::new() } } fn add_route (&mut self, path: String, handler: fn(request:

How to fix: expected concrete lifetime, but found bound lifetime parameter

倖福魔咒の 提交于 2019-12-31 07:18:05
问题 I'm currently pulling my hear out over this one. I tried to shrink it down to a minimal reproducible example. struct Request; struct ResponseWriter<'a> { dummy: &'a () } #[deriving(Clone)] pub struct RouteStore{ pub routes: Vec<Route>, } #[deriving(Clone)] struct Route { path: String, handler: fn(request: &Request, response: &mut ResponseWriter) } impl RouteStore { pub fn new () -> RouteStore { RouteStore { routes: Vec::new() } } fn add_route (&mut self, path: String, handler: fn(request:

How do I handle an FFI unsized type that could be owned or borrowed?

旧城冷巷雨未停 提交于 2019-12-31 01:54:31
问题 c_strange_t is an opaque C type that is only seen behind a pointer. When wrapping this type, there are times when it is our responsibility to free memory using c_free_strange_t(*c_strange_t) , and other times when we are not responsible for freeing the data, we are only responsible for accurately controlling the lifetime. It would be ergonomic if this type could be mapped into 2 types in Rust that work in a similar way to str and String , where there is impl Deref<Target=str> for String . The

What does it mean for a trait to have a lifetime parameter?

孤人 提交于 2019-12-30 10:57:06
问题 I understand how lifetime parameters apply to functions and structs, but what does it mean for a trait to have a lifetime parameter? Is it a shortcut to introduce a lifetime parameter to its methods, or is it something else? 回答1: If you have a trait with a lifetime bound, then implementors of the trait can participate in the same lifetime. Concretely, this allows you to store references with that lifetime. It is not a shortcut for specifying lifetimes on member methods, and insanity and

How do you create a generic function in Rust with a trait requiring a lifetime?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-30 10:06:08
问题 I am trying to write a trait which works with a database and represents something which can be stored. To do this, the trait inherits from others, which includes the serde::Deserialize trait. trait Storable<'de>: Serialize + Deserialize<'de> { fn global_id() -> &'static [u8]; fn instance_id(&self) -> Vec<u8>; } struct Example { a: u8, b: u8 } impl<'de> Storable<'de> for Example { fn global_id() -> &'static [u8] { b"p" } fn instance_id(&self) -> Vec<u8> { vec![self.a, self.b] } } Next, I am

Borrow checker doesn't realize that `clear` drops reference to local variable

无人久伴 提交于 2019-12-29 08:27:26
问题 The following code reads space-delimited records from stdin, and writes comma-delimited records to stdout. Even with optimized builds it's rather slow (about twice as slow as using, say, awk). use std::io::BufRead; fn main() { let stdin = std::io::stdin(); for line in stdin.lock().lines().map(|x| x.unwrap()) { let fields: Vec<_> = line.split(' ').collect(); println!("{}", fields.join(",")); } } One obvious improvement would be to use itertools to join without allocating a vector (the collect