lifetime

Is it safe to make a const reference member to a temporary variable?

人走茶凉 提交于 2021-01-18 19:14:22
问题 I've tried to code like this several times: struct Foo { double const& f; Foo(double const& fx) : f(fx) { printf("%f %f\n", fx, this->f); // 125 125 } double GetF() const { return f; } }; int main() { Foo p(123.0 + 2.0); printf("%f\n", p.GetF()); // 0 return 0; } But it doesn't crash at all. I've also used valgrind to test the program but no error or warning occured. So, I assume that the compiler automatically generated a code directing the reference to another hidden variable. But I'm

Lifetime issue when using the Any trait to get references to structs containing references

谁都会走 提交于 2020-12-26 04:28:26
问题 I ran into a lifetime problem with a little game. The below code represents a very boiled down version of the update loop. I need the container mutable reference to get references to other game objects or to create new ones or trigger a functionality. For that reason, I need the Any trait to be able to cast the trait to a struct, so in my GameObj trait I added an as_any method, but this resulted in a lifetime issue. use std::any::Any; trait GameObj<'a> { fn as_any<'b>(&'b self) -> &'b (dyn

Lifetime issue when using the Any trait to get references to structs containing references

房东的猫 提交于 2020-12-26 04:27:59
问题 I ran into a lifetime problem with a little game. The below code represents a very boiled down version of the update loop. I need the container mutable reference to get references to other game objects or to create new ones or trigger a functionality. For that reason, I need the Any trait to be able to cast the trait to a struct, so in my GameObj trait I added an as_any method, but this resulted in a lifetime issue. use std::any::Any; trait GameObj<'a> { fn as_any<'b>(&'b self) -> &'b (dyn

Why do I need to collect into a vector when using `flat_map`?

时光怂恿深爱的人放手 提交于 2020-12-08 08:44:46
问题 I'm working on Project Euler 96 to teach myself Rust. I've written this code to read in the file and convert it into a vector of integers (Playground). let file = File::open(&args[1]).expect("Sudoku file not found"); let reader = BufReader::new(file); let x = reader .lines() .map(|x| x.unwrap()) .filter(|x| !x.starts_with("Grid")) .flat_map(|s| s.chars().collect::<Vec<_>>()) // <-- collect here! .map(|x| x.to_digit(10).unwrap()) .collect::<Vec<_>>(); This all works fine but I'm puzzled why I

Rust, how to return reference to something in a struct that lasts as long as the struct?

给你一囗甜甜゛ 提交于 2020-12-07 03:41:07
问题 I am porting a compiler I wrote to Rust. In it, I have an enum Entity which represents things like functions and variables: pub enum Entity<'a> { Variable(VariableEntity), Function(FunctionEntity<'a>) // Room for more later. } I then have a struct Scope which is responsible for holding on to these entities in a hash map, where the key is the name given by the programmer to the entity. (For example, declaring a function named sin would put an Entity into the hash map at the key sin .) pub

Rust, how to return reference to something in a struct that lasts as long as the struct?

为君一笑 提交于 2020-12-07 03:40:51
问题 I am porting a compiler I wrote to Rust. In it, I have an enum Entity which represents things like functions and variables: pub enum Entity<'a> { Variable(VariableEntity), Function(FunctionEntity<'a>) // Room for more later. } I then have a struct Scope which is responsible for holding on to these entities in a hash map, where the key is the name given by the programmer to the entity. (For example, declaring a function named sin would put an Entity into the hash map at the key sin .) pub

Confused about variable lifetime in tokio::spawn(async move

匆匆过客 提交于 2020-12-06 04:34:25
问题 I am new to rust and tokio async, and I am trying to compile the following seemingly straightforward code: async fn network_handler(network_config: &config::NetworkConfig) -> Result<(), Error> { Ok(()) } pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> { let network_config_copy = network_config.clone(); tokio::spawn(async move { network_handler(&network_config_copy).await }).await? } But the compiler complains: error: cannot infer an appropriate lifetime --> src

Confused about variable lifetime in tokio::spawn(async move

你。 提交于 2020-12-06 04:34:21
问题 I am new to rust and tokio async, and I am trying to compile the following seemingly straightforward code: async fn network_handler(network_config: &config::NetworkConfig) -> Result<(), Error> { Ok(()) } pub async fn run(network_config: &config::NetworkConfig) -> Result<(), Error> { let network_config_copy = network_config.clone(); tokio::spawn(async move { network_handler(&network_config_copy).await }).await? } But the compiler complains: error: cannot infer an appropriate lifetime --> src