lifetime

“error: closure may outlive the current function” but it will not outlive it

做~自己de王妃 提交于 2019-11-30 18:24:14
When I try to compile the following code: fn main() { (...) let mut should_end = false; let mut input = Input::new(ctx); input.add_handler(Box::new(|evt| { match evt { &Event::Quit{..} => { should_end = true; } _ => {} } })); while !should_end { input.handle(); } } pub struct Input { handlers: Vec<Box<FnMut(i32)>>, } impl Input { pub fn new() -> Self { Input {handlers: Vec::new()} } pub fn handle(&mut self) { for a in vec![21,0,3,12,1] { for handler in &mut self.handlers { handler(a); } } } pub fn add_handler(&mut self, handler: Box<FnMut(i32)>) { self.handlers.push(handler); } } I get this

“error: closure may outlive the current function” but it will not outlive it

一曲冷凌霜 提交于 2019-11-30 16:50:23
问题 When I try to compile the following code: fn main() { (...) let mut should_end = false; let mut input = Input::new(ctx); input.add_handler(Box::new(|evt| { match evt { &Event::Quit{..} => { should_end = true; } _ => {} } })); while !should_end { input.handle(); } } pub struct Input { handlers: Vec<Box<FnMut(i32)>>, } impl Input { pub fn new() -> Self { Input {handlers: Vec::new()} } pub fn handle(&mut self) { for a in vec![21,0,3,12,1] { for handler in &mut self.handlers { handler(a); } } }

Understanding ASP.Net session life time

孤街醉人 提交于 2019-11-30 14:22:17
问题 I am confused about ASP or ASP.Net session life time (or life cycle) concepts. More specifically, my confusions are: How does IIS decide when a new session starts and an existing session ends? Especially how does IIS decide whether a session continues or ends when we call redirect code? How can we set session expire time? (Currently I only know to set it through web.config sessionState item.) Is it possible for one session to access another session's variables? 回答1: Session starts because the

How do I efficiently build a vector and an index of that vector while processing a data stream?

痞子三分冷 提交于 2019-11-30 14:00:46
I have a struct Foo : struct Foo { v: String, // Other data not important for the question } I want to handle a data stream and save the result into Vec<Foo> and also create an index for this Vec<Foo> on the field Foo::v . I want to use a HashMap<&str, usize> for the index, where the keys will be &Foo::v and the value is the position in the Vec<Foo> , but I'm open to other suggestions. I want to do the data stream handling as fast as possible, which requires not doing obvious things twice. For example, I want to: allocate a String only once per one data stream reading not search the index

Why can't I use a key function that returns a reference when sorting a vector with sort_by_key?

孤街浪徒 提交于 2019-11-30 12:46:11
I'm trying to sort a Vec<String> using a key function that returns references to the strings in the vector. A contrived example is to use the identity function as key function (which of course is useless, but it's the minimal example to reproduce my problem): fn key(x: &String) -> &String { x } Now given items: Vec<String> , I'd like to be able to do items.sort_by_key(key); This gives the following error: error[E0271]: type mismatch resolving `for<'r> <fn(&std::string::String) -> &std::string::String {main::key} as std::ops::FnOnce<(&'r std::string::String,)>>::Output == _` --> src/main.rs:19

What is the lifetime of class static variables in C++?

假如想象 提交于 2019-11-30 12:12:44
问题 If I have a class called Test :: class Test { static std::vector<int> staticVector; }; when does staticVector get constructed and when does it get destructed ? Is it with the instantiation of the first object of Test class, or just like regular static variables ? Just to clarify, this question came to my mind after reading Concepts of Programming Languages (Sebesta Ch-5.4.3.1) and it says :: Note that when the static modifier appears in the declaration of a variable in a class definition in C

Understanding ASP.Net session life time

微笑、不失礼 提交于 2019-11-30 10:08:50
I am confused about ASP or ASP.Net session life time (or life cycle) concepts. More specifically, my confusions are: How does IIS decide when a new session starts and an existing session ends? Especially how does IIS decide whether a session continues or ends when we call redirect code? How can we set session expire time? (Currently I only know to set it through web.config sessionState item.) Is it possible for one session to access another session's variables? Session starts because the request does not contain a session cookie or the session cookie it does contain no longer maps to a session

Full-expression boundaries and lifetime of temporaries [duplicate]

时光怂恿深爱的人放手 提交于 2019-11-30 08:27:12
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: C++: Life span of temporary arguments? It is said that temporary variables are destroyed as the last step in evaluating the full-expression, e.g. bar( foo().c_str() ); temporary pointer lives until bar returns, but what for the baz( bar( foo().c_str() ) ); is it still lives until bar returns, or baz return means full-expression end here, compilers I checked destruct objects after baz returns, but can I rely on

Lifetime woes when using threads

一曲冷凌霜 提交于 2019-11-30 05:00:05
问题 I'm having a hard time getting this to compile: use std::thread::{self, JoinHandle}; struct Foo<'c> { foo: &'c str, } impl<'c> Foo<'c> { fn use_in_another_thread<F>(self, mut cb: F) -> JoinHandle<Foo<'c>> where F: FnOnce(&mut Foo), F: Send { thread::spawn(move || { cb(&mut self); self }) } } fn main() {} As far as I can see the lifetimes are sound, but I'm getting errors... error[E0477]: the type `[closure@src/main.rs:12:23: 15:10 cb:F, self:Foo<'c>]` does not fulfill the required lifetime --

Using a `let` binding to increase a values lifetime

£可爱£侵袭症+ 提交于 2019-11-30 03:02:46
问题 I wrote the following code to read an array of integers from stdin : use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { let xs: Vec<i32> = line.unwrap() .trim() .split(' ') .map(|s| s.parse().unwrap()) .collect(); println!("{:?}", xs); } } This worked fine, however, I felt the let xs line was a bit long, so I split it into two: use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); for line in stdin.lock().lines() { let ss = line