lifetime

Why can the lifetimes not be elided in a struct definition?

余生颓废 提交于 2019-11-26 11:37:45
问题 struct Point { x: u32, y: u32, } struct Line<\'a> { start: &\'a Point, end: &\'a Point, } Here, the only possible option for the start and end fields is to have a lifetime the same or longer than the Line variable that contains them. I can\'t even imagine how one will go about using a lifetime specifier to say that the fields have a shorter lifespan. Why do I have to explicitly specify a lifetime here? Is elision not possible in this situation and if so why not? 回答1: When you define a struct,

Why is adding a lifetime to a trait with the plus operator (Iterator<Item = &Foo> + &#39;a) needed?

*爱你&永不变心* 提交于 2019-11-26 11:24:38
I'm applying a closure on the iterator and I want to use stable, so I want to return a boxed Iterator . The obvious way to do so is the following: struct Foo; fn into_iterator(myvec: &Vec<Foo>) -> Box<Iterator<Item = &Foo>> { Box::new(myvec.iter()) } This fails because the borrow checker cannot infer the appropriate lifetimes. After some research, I've found Correct way to return an Iterator? , which brought me to adding + 'a : fn into_iterator<'a>(myvec: &'a Vec<Foo>) -> Box<Iterator<Item = &'a Foo> + 'a> { Box::new(myvec.iter()) } But I don't understand What this does And why it is needed

Why are explicit lifetimes needed in Rust?

谁说我不能喝 提交于 2019-11-26 10:15:11
I was reading the lifetimes chapter of the Rust book, and I came across this example for a named/explicit lifetime: struct Foo<'a> { x: &'a i32, } fn main() { let x; // -+ x goes into scope // | { // | let y = &5; // ---+ y goes into scope let f = Foo { x: y }; // ---+ f goes into scope x = &f.x; // | | error here } // ---+ f and y go out of scope // | println!("{}", x); // | } // -+ x goes out of scope It's quite clear to me that the error being prevented by the compiler is the use-after-free of the reference assigned to x : after the inner scope is done, f and therefore &f.x become invalid,

How does the lifetime work on constant strings / string literals?

女生的网名这么多〃 提交于 2019-11-26 10:03:36
问题 I read the tutorial on the official website and I have some questions on the lifetime of constant strings / string literals. I get an error when I write the following code: fn get_str() -> &str { \"Hello World\" } error: error[E0106]: missing lifetime specifier --> src/main.rs:1:17 | 1 | fn get_str() -> &str { | ^ expected lifetime parameter | = help: this function\'s return type contains a borrowed value, but there is no value for it to be borrowed from = help: consider giving it a \'static

“borrowed value does not live long enough” when using the builder pattern

眉间皱痕 提交于 2019-11-26 10:03:23
问题 I have the following code: pub struct Canvas<\'a> { width: isize, height: isize, color: Color, surface: Surface, texture: Texture, renderer: &\'a Renderer, } impl<\'a> Canvas<\'a> { pub fn new(width: isize, height: isize, renderer: &\'a Renderer) -> Canvas<\'a> { let color = Color::RGB(0, 30, 0); let mut surface = core::create_surface(width, height); let texture = Canvas::gen_texture(&mut surface, width, height, color, renderer); Canvas { width: width, height: height, color: color, surface:

When returning the outcome of consuming a StdinLock, why was the borrow to stdin retained?

寵の児 提交于 2019-11-26 10:01:52
问题 Given the following function: use std::io::{BufRead, stdin}; fn foo() -> usize { let stdin = stdin(); let stdinlock = stdin.lock(); stdinlock .lines() .count() } This fails to compile with the following error: error: `stdin` does not live long enough --> src/main.rs:12:1 | 7 | let stdinlock = stdin.lock(); | ----- borrow occurs here ... 11 | } | ^ `stdin` dropped here while still borrowed | = note: values in a scope are dropped in the opposite order they are created I find this surprising

C++ constant reference lifetime (container adaptor)

蓝咒 提交于 2019-11-26 09:09:59
问题 I have code that looks like this: class T {}; class container { const T &first, T &second; container(const T&first, const T & second); }; class adapter : T {}; container(adapter(), adapter()); I thought lifetime of constant reference would be lifetime of container. However, it appears otherwise, adapter object is destroyed after container is created, leaving dangling reference. What is the correct lifetime? is stack scope of adapter temporary object the scope of container object or of

Iterator returning items by reference, lifetime issue

我只是一个虾纸丫 提交于 2019-11-26 08:53:53
问题 I have a lifetime issue, I\'m trying to implement an iterator returning its items by reference, here is the code: struct Foo { d: [u8; 42], pos: usize } impl<\'a> Iterator<&\'a u8> for Foo { fn next<\'a>(&\'a mut self) -> Option<&\'a u8> { let r = self.d.get(self.pos); if r.is_some() { self.pos += 1; } r } } fn main() { let mut x = Foo { d: [1; 42], pos: 0 }; for i in x { println!(\"{}\", i); } } However this code doesn\'t compile properly, I get an issue related to the lifetime of parameters

The compiler suggests I add a &#39;static lifetime because the parameter type may not live long enough, but I don&#39;t think that&#39;s what I want

自作多情 提交于 2019-11-26 08:48:25
I'm trying to implement something that looks like this minimal example: trait Bar<T> {} struct Foo<T> { data: Vec<Box<Bar<T>>>, } impl<T> Foo<T> { fn add<U: Bar<T>>(&mut self, x: U) { self.data.push(Box::new(x)); } } Since Rust defaults to (as far as I can tell) pass-by-ownership, my mental model thinks this should work. The add method takes ownership of object x and is able to move this object into a Box because it knows the full type U (and not just trait Bar<T> ). Once moved into a Box , the lifetime of the item inside the box should be tied to the actual lifetime of the box (e.g., when pop

What is lifetime elision in very simple terms?

China☆狼群 提交于 2019-11-26 08:30:21
问题 From the Rust documentation: Rust supports powerful local type inference in the bodies of functions, but it deliberately does not perform any reasoning about types for item signatures. However, for ergonomic reasons, a very restricted secondary inference algorithm called “lifetime elision” does apply when judging lifetimes. Lifetime elision is concerned solely with inferring lifetime parameters using three easily memorizable and unambiguous rules. This means lifetime elision acts as a