lifetime

Define a trait with a function that returns an associated type with the same lifetime as one parameter

馋奶兔 提交于 2019-12-23 12:35:03
问题 I'm trying to define a trait with a function that returns an associated type with the same lifetime as one parameter. Conceptually something like the following (which doesn't work: lifetime parameter not allowed on this type [Self::Output] ): trait Trait { type Output; fn get_output<'a>(&self, input: &'a i32) -> Self::Output<'a>; } I found several questions about lifetimes for associated types on Stack Overflow and the Internet, but none seem to help. Some suggested defining the lifetime on

Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct

人走茶凉 提交于 2019-12-23 10:41:22
问题 When I try to compile this code: pub struct Context<'a> { pub outer: Option<&'a mut Context<'a>>, } impl<'a> Context<'a> { pub fn inside(outer: &'a mut Context) -> Context<'a> { Context { outer: Some(outer) } } } I'm getting this error: error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements --> src/main.rs:7:9 | 7 | Context { outer: Some(outer) } | ^^^^^^^ | note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl

Cannot infer an appropriate lifetime due to conflicting requirements in a recursive struct

狂风中的少年 提交于 2019-12-23 10:40:11
问题 When I try to compile this code: pub struct Context<'a> { pub outer: Option<&'a mut Context<'a>>, } impl<'a> Context<'a> { pub fn inside(outer: &'a mut Context) -> Context<'a> { Context { outer: Some(outer) } } } I'm getting this error: error[E0495]: cannot infer an appropriate lifetime for lifetime parameter `'a` due to conflicting requirements --> src/main.rs:7:9 | 7 | Context { outer: Some(outer) } | ^^^^^^^ | note: first, the lifetime cannot outlive the lifetime 'a as defined on the impl

Lifetime of JavaScript variables

别来无恙 提交于 2019-12-23 07:38:08
问题 What is the lifetime of a variable in JavaScript, declared with "var". I am sure, it is definitely not according to expectation. <script> function(){ var a; var fun=function(){ // a is accessed and modified } }(); </script> Here how and when does JavaScript garbage collect the variable a ? Since a is a part of the closure of the inner function, it ideally should never get garbage collected, since the inner function fun , may be passed as a reference to an external context. So fun should still

How to allocate a string before you know how big it needs to be

烂漫一生 提交于 2019-12-23 07:05:38
问题 I'm sure this is a beginners mistake. My code is: ... let mut latest_date : Option<Date<Local>> = None; let mut latest_datetime : Option<DateTime<Local>> = None; let mut latest_activity : Option<&str> = None; for wrapped_line in reader.lines() { let line = wrapped_line.unwrap(); println!("line: {}", line); if date_re.is_match(&line) { let captures = date_re.captures(&line).unwrap(); let year = captures.at(1).unwrap().parse::<i32>().unwrap(); let month = captures.at(2).unwrap().parse::<u32>()

Why does a MutexGuard require a lifetime parameter in structs but not in function return types? [duplicate]

非 Y 不嫁゛ 提交于 2019-12-22 18:26:32
问题 This question already has answers here : Why can the lifetimes not be elided in a struct definition? (2 answers) What is lifetime elision in very simple terms? (1 answer) Closed last year . I'd like to move a MutexGuard around. Returning a MutexGuard from a function works fine without giving a lifetime parameter. But when packing the guard into a struct, the compiler demands a lifetime parameter for the guard. The following code compiles without errors: struct Queue { queue: Mutex<Vec<i32>>,

Why does a MutexGuard require a lifetime parameter in structs but not in function return types? [duplicate]

£可爱£侵袭症+ 提交于 2019-12-22 18:26:05
问题 This question already has answers here : Why can the lifetimes not be elided in a struct definition? (2 answers) What is lifetime elision in very simple terms? (1 answer) Closed last year . I'd like to move a MutexGuard around. Returning a MutexGuard from a function works fine without giving a lifetime parameter. But when packing the guard into a struct, the compiler demands a lifetime parameter for the guard. The following code compiles without errors: struct Queue { queue: Mutex<Vec<i32>>,

What is the lifetime of the class data member which const reference to a rvalue?

只愿长相守 提交于 2019-12-22 08:57:59
问题 Generally this discussion is up to the local function variable only: void foo (const int &i) { // use i till foo() ends } foo(3); But, does this rule applies to the class member also ? struct A { const int &a; A () : a(3) {} // version 1 A (const int &i) : a(i) {} // version 2 }; Now A used as, { return ()? new A : new A(3) : new A(some_local_variable); } Will the contents of a remain same through out the life time of the all 3 new ly allocated A ? 回答1: The C++03 standard ( Section "12.2/5

Are global variables faster than local variables in C? [closed]

孤人 提交于 2019-12-22 08:53:40
问题 Closed . This question needs to be more focused. It is not currently accepting answers. Want to improve this question? Update the question so it focuses on one problem only by editing this post. Closed 3 years ago . I had a couple thoughts on this. The first is that allocating global variables may be faster, since they are only allocated once, at the time the program is first spawned, whereas local variables must be allocated every time a function is called. My second thought is that since

Where can I find a detailed view of the lifecycle of a Windows Service as developed in .NET?

梦想的初衷 提交于 2019-12-22 06:46:31
问题 Where can I find a detailed view of the lifecycle of a Windows Service as developed in .NET? I put my question this way because I am not sure that a detailed enough description can be posted here, but if you think you can please feel free to try. An example of an incorrect answer would be a paste of the description from the MSDN page: Introduction to Windows Service Applications. It is not nearly detailed enough. For instance, is a service unloaded out of memory, and therefor the Dispose