lifetime

How do I implement the Add trait for a reference to a struct?

旧时模样 提交于 2019-11-26 02:57:56
问题 I made a two element Vector struct and I want to overload the + operator. I made all my functions and methods take references, rather than values, and I want the + operator to work the same way. impl Add for Vector { fn add(&self, other: &Vector) -> Vector { Vector { x: self.x + other.x, y: self.y + other.y, } } } Depending on which variation I try, I either get lifetime problems or type mismatches. Specifically, the &self argument seems to not get treated as the right type. I have seen

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

我的未来我决定 提交于 2019-11-26 02:24:52
问题 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<dyn Iterator<Item = &Foo>> { Box::new(myvec.iter()) } This fails because the borrow checker cannot infer the appropriate lifetimes. After some research, I\'ve found What is the correct way to return an Iterator (or any other trait)?, which brought me to adding + \'a : fn into_iterator<\'a>

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 01:43:58
问题 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 ,

Lifetime of temporaries

喜欢而已 提交于 2019-11-26 00:25:51
问题 The following code works fine, but why is this correct code? Why is the \"c_str()\" pointer of the temporary returned by foo() valid? I thought, that this temporary is already destroyed when bar() is entered - but it doesn\'t seem to be like this. So, now I assume that the temporary returned by foo() will be destroyed after the call to bar() - is this correct? And why? std::string foo() { std::string out = something...; return out; } void bar( const char* ccp ) { // do something with the

Why can&#39;t I store a value and a reference to that value in the same struct?

非 Y 不嫁゛ 提交于 2019-11-25 23:55:56
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<\'a>(Thing, &\'a u32); fn make_combined<\'a>() -> Combined<\'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<\'a>(Thing, &\'a Thing); fn make_combined<\'a>() -> Combined<\'a> { let thing = Thing:

How do I write an iterator that returns references to itself?

女生的网名这么多〃 提交于 2019-11-25 23:50:24
问题 I am having trouble expressing the lifetime of the return value of an Iterator implementation. How can I compile this code without changing the return value of the iterator? I\'d like it to return a vector of references. It is obvious that I am not using the lifetime parameter correctly but after trying various ways I just gave up, I have no idea what to do with it. use std::iter::Iterator; struct PermutationIterator<T> { vs: Vec<Vec<T>>, is: Vec<usize>, } impl<T> PermutationIterator<T> { fn

What is the lifetime of a static variable in a C++ function?

天涯浪子 提交于 2019-11-25 22:32:55
问题 If a variable is declared as static in a function\'s scope it is only initialized once and retains its value between function calls. What exactly is its lifetime? When do its constructor and destructor get called? void foo() { static string plonk = \"When will I die?\"; } 回答1: The lifetime of function static variables begins the first time [0] the program flow encounters the declaration and it ends at program termination. This means that the run-time must perform some book keeping in order to

What are non-lexical lifetimes?

﹥>﹥吖頭↗ 提交于 2019-11-25 21:56:35
问题 Rust has an RFC related to non-lexical lifetimes which has been approved to be implemented in the language for a long time. Recently, Rust\'s support of this feature has improved a lot and is considered complete. My question is: what exactly is a non-lexical lifetime? 回答1: It's easiest to understand what non-lexical lifetimes are by understanding what lexical lifetimes are. In versions of Rust before non-lexical lifetimes are present, this code will fail: fn main() { let mut scores = vec![1,

Is there any way to return a reference to a variable created in a function?

喜欢而已 提交于 2019-11-25 21:42:30
问题 I want to write a program that will write a file in 2 steps. It is likely that the file may not exist before the program is run. The filename is fixed. The problem is that OpenOptions.new().write() can fail. In that case, I want to call a custom function trycreate() . The idea is to create the file instead of opening it and return a handle. Since the filename is fixed, trycreate() has no arguments and I cannot set a lifetime of the returned value. How can I resolve this problem? use std::io: