lifetime

Why does my trait need a lifetime parameter?

泄露秘密 提交于 2020-05-13 04:56:40
问题 Being a Rust newbie, I probably somewhat naively started with this: ... pub trait Decode<T> { fn decode_from<R: io::Read + ?Sized>(&mut self, stream: &mut R) -> T; } pub struct MQTTFrame<'a> { pub payload: &'a Vec<u8>, } pub struct MQTTFrameDecoder<'a> { pub payload: &'a mut Vec<u8>, } impl<'a> Decode<MQTTFrame<'a>> for MQTTFrameDecoder<'a> { fn decode_from<R: io::Read + ?Sized>(&mut self, stream: &mut R) -> MQTTFrame<'a> { stream.read(&mut self.payload); MQTTFrame{ payload: self.payload } }

Is there a way to have a Rust closure that moves only some variables into it?

给你一囗甜甜゛ 提交于 2020-05-06 17:46:53
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

Is there a way to have a Rust closure that moves only some variables into it?

依然范特西╮ 提交于 2020-05-06 17:45:17
问题 I have a general struct with settings and an extra variable setting that I want to tune and play around with. For all possible values in an integer range, I want to start a (scoped) thread with this variable set to that value. Depending on this value, they do slightly different work. Each of these threads should be able to read the general settings struct. use crossbeam; // 0.7.3 struct Settings { // ... many fields } const MAX_FEASIBLE_SCORE: u8 = 10; fn example(settings: Settings) {

Late destruction of function parameters

瘦欲@ 提交于 2020-04-05 15:53:54
问题 According to 5.2.2/4 "Function call" in n4640 (8.2.2/4 in n4659) function parameters are created and destroyed in the context of the caller. And implementations are allowed to delay the destruction of function parameters to the end of the enclosing full expression (as an implementation-defined feature). Note that the choice is not unspecified , but rather implementation-defined . ( It is not entirely clear how this agrees with 3.3.3 "Block scope" (6.3.3 in n4659), which seems to imply that

Variable lifetime

做~自己de王妃 提交于 2020-03-21 20:27:09
问题 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 happends to variable number? Here is another example with creating instance of class: 7 public void myMethod() 8 { 9 Customer myClient; 10 myClient = new Customer(); 11 } When it goes outside of code block (line 11) what happends to object reference myClient? I

String's lifetime when returning Vec<&str> [duplicate]

瘦欲@ 提交于 2020-03-16 06:14:09
问题 This question already has answers here : Return local String as a slice (&str) (4 answers) Closed 4 years ago . Simple code: fn foo() -> Vec<&'static str> { let mut vec = Vec::new(); let mut string = String::new(); // doing something with string... vec.push(string.as_str()); return vector; // error here: string doesn't live long enough } I have problem that I need to process with string and return it in Vec as str. Problem is that binding string doesn't live long enough, since it goes out of

String's lifetime when returning Vec<&str> [duplicate]

前提是你 提交于 2020-03-16 06:13:04
问题 This question already has answers here : Return local String as a slice (&str) (4 answers) Closed 4 years ago . Simple code: fn foo() -> Vec<&'static str> { let mut vec = Vec::new(); let mut string = String::new(); // doing something with string... vec.push(string.as_str()); return vector; // error here: string doesn't live long enough } I have problem that I need to process with string and return it in Vec as str. Problem is that binding string doesn't live long enough, since it goes out of

How to define lifetimes on a Fn trait bound returning references?

巧了我就是萌 提交于 2020-02-05 04:34:29
问题 I'm trying to create a struct with a field, generic over F where F implements something like: Fn(&mut Compiler, &[Token]) -> &Token . The only issue is, I'm not sure how I define lifetimes on the Fn trait which satisfy the constraint that the returned &Token references data in the &[Token] slice supplied as an argument. Everything I've tried has thrown cryptic errors thus far. Here is an MVCE which demonstrates the code (without any lifetimes): struct Compiler; #[derive(Debug)] struct Token

HRTB: Concrete type bounded by a trait containing lifetime type parameter vs bounded by only a lifetime type parameter

一世执手 提交于 2020-02-04 03:58:26
问题 This is actually an offshoot of this SO question. Consider the following code: trait Trait<'b> { fn func(&'b self) {} } struct Struct {} impl<'s> Trait<'s> for Struct {} fn test<'s, T:'s>(t: T) where T: Trait<'s>, { t.func(); } It fails, as the compiler sees that t lives for less than the 's and 's is set by the caller (i.e longer than the stack-frame of the func ) and traits are invariant over type parameters. However, if I introduce HRTB (Higher Rank Trait Bounds) here, the code compiles:

Is there a way to obtain elided lifetime parameters from the Rust compiler?

依然范特西╮ 提交于 2020-02-04 02:40:53
问题 Given a Rust program, which compiles correctly, can I get the compiler to tell me what the elided lifetimes were inferred to be? 回答1: The cases where the compiler (currently 1 ) can allow elided lifetimes are actually so simple that there isn't much the compiler could tell you about what it inferred: Given a function, all elided lifetimes have the same value. The compiler doesn't accept elided lifetimes in cases where it would have a choice to make. The exception is in methods, but tying all