lifetime

Will (global) static variables be destroyed at program end? [duplicate]

北城以北 提交于 2019-12-28 19:16:47
问题 This question already has answers here : Closed 7 years ago . Possible Duplicate: Does C++ call destructors for global and class static variables? What is the lifetime of global MyClass myclass; global static MyClass myclass; global const MyClass myclass; global static const MyClass myclass; function local static MyClass myclass; when its initialization actually occured global static constexpr MyClass myclass; in C++11 and especially will they be destroyed on regular program end (i.e. main is

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

时间秒杀一切 提交于 2019-12-28 15:36:09
问题 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

How to operate on 2 mutable slices of a Rust array?

廉价感情. 提交于 2019-12-28 07:02:21
问题 I have a function that needs to operate on two parts of a single array. The purpose is to be able to build an #[nostd] allocator that can return a variable slice of a bigger array to the caller and hang on to the remainder of the array for future allocations. Here's example code that fails: fn split<'a>(mut item: &'a mut [i32], place: usize) -> (&'a mut [i32], &'a mut [i32]) { (&mut item[0..place], &mut item[place..]) } fn main() { let mut mem: [i32; 2048] = [1; 2048]; let (mut array0, mut

How do I specify lifetime parameters in an associated type?

白昼怎懂夜的黑 提交于 2019-12-28 04:00:14
问题 I have this trait and simple structure: use std::path::{Path, PathBuf}; trait Foo { type Item: AsRef<Path>; type Iter: Iterator<Item = Self::Item>; fn get(&self) -> Self::Iter; } struct Bar { v: Vec<PathBuf>, } I would like to implement the Foo trait for Bar : impl Foo for Bar { type Item = PathBuf; type Iter = std::slice::Iter<PathBuf>; fn get(&self) -> Self::Iter { self.v.iter() } } However I'm getting this error: error[E0106]: missing lifetime specifier --> src/main.rs:16:17 | 16 | type

Why does my variable not live long enough?

为君一笑 提交于 2019-12-27 16:07:32
问题 I have a simple piece of code that is supposed to read a file into a vector by lines use std::io::{self, Read}; use std::fs::File; fn file_to_vec(filename: &str) -> Result<Vec<&str>, io::Error> { let mut file = try!(File::open(filename)); let mut string = String::new(); try!(file.read_to_string(&mut string)); string.replace("\r", ""); let data: Vec<&str> = string.split('\n').collect(); Ok(data) } fn main() {} I am getting the following error: error[E0597]: `string` does not live long enough -

Why does my variable not live long enough?

独自空忆成欢 提交于 2019-12-27 16:06:51
问题 I have a simple piece of code that is supposed to read a file into a vector by lines use std::io::{self, Read}; use std::fs::File; fn file_to_vec(filename: &str) -> Result<Vec<&str>, io::Error> { let mut file = try!(File::open(filename)); let mut string = String::new(); try!(file.read_to_string(&mut string)); string.replace("\r", ""); let data: Vec<&str> = string.split('\n').collect(); Ok(data) } fn main() {} I am getting the following error: error[E0597]: `string` does not live long enough -

How can multiple struct fields be generics that use the same higher-kinded lifetime?

会有一股神秘感。 提交于 2019-12-25 09:57:08
问题 My struct ReadingState takes the function recv_dgram as argument in its new() method. recv_dgram takes as an argument a buffer with some lifetime 'r , and returns a Future of a certain type. The Item of this future contains the buffer that was fed as an argument, with the same lifetime 'r . This is how ReadingState looks like: struct FragMsgReceiver<'a, A, FUNC: 'a> where FUNC: for<'r> FnMut(&'r [u8]) -> Future<Item = (&'r [u8], usize, A), Error = io::Error>, { frag_state_machine:

How can multiple struct fields be generics that use the same higher-kinded lifetime?

送分小仙女□ 提交于 2019-12-25 09:56:21
问题 My struct ReadingState takes the function recv_dgram as argument in its new() method. recv_dgram takes as an argument a buffer with some lifetime 'r , and returns a Future of a certain type. The Item of this future contains the buffer that was fed as an argument, with the same lifetime 'r . This is how ReadingState looks like: struct FragMsgReceiver<'a, A, FUNC: 'a> where FUNC: for<'r> FnMut(&'r [u8]) -> Future<Item = (&'r [u8], usize, A), Error = io::Error>, { frag_state_machine:

How can I return a reference to a local variable specifying that its lifetime is the same as self?

僤鯓⒐⒋嵵緔 提交于 2019-12-25 09:42:11
问题 I'd like to write some code like below struct SomeData(u8, u8); impl SomeData { fn to_bytes(&self) -> &[u8] { let mut bytes: [u8; 16] = [0; 16]; // fill up buffer with some data in `SomeData`. bytes[0] = self.0; bytes[1] = self.1; // return slice &bytes[..] } } I know the reason why above code doesn't work. How can I return a reference specifying that its lifetime is the same as self ? 回答1: Explicit lifetime annotation of a reference cannot extend lifetime of an object it refers to. bytes is

How can I return a reference to a local variable specifying that its lifetime is the same as self?

让人想犯罪 __ 提交于 2019-12-25 09:42:03
问题 I'd like to write some code like below struct SomeData(u8, u8); impl SomeData { fn to_bytes(&self) -> &[u8] { let mut bytes: [u8; 16] = [0; 16]; // fill up buffer with some data in `SomeData`. bytes[0] = self.0; bytes[1] = self.1; // return slice &bytes[..] } } I know the reason why above code doesn't work. How can I return a reference specifying that its lifetime is the same as self ? 回答1: Explicit lifetime annotation of a reference cannot extend lifetime of an object it refers to. bytes is