lifetime

What is the lifetime of the class data member which const reference to a rvalue?

喜欢而已 提交于 2019-12-05 19:38:09
Generally this discussion is up to the local function variable only: void foo (const int &i) { // use i till foo() ends } foo(3); But, does this rule applies to the class member also ? struct A { const int &a; A () : a(3) {} // version 1 A (const int &i) : a(i) {} // version 2 }; Now A used as, { return ()? new A : new A(3) : new A(some_local_variable); } Will the contents of a remain same through out the life time of the all 3 new ly allocated A ? The C++03 standard ( Section "12.2/5 Temporary objects" ) answers your question aptly: The temporary to which the reference is bound or the

Destructor called on assignment between (stack) variables?

北城余情 提交于 2019-12-05 17:39:00
matrix m1(5,5); matrix m2(5,5); m1 = matrix(m2); For the above code (for an arbitrary class, matrix), will the destructor be called for the information associated with m1, when the information of m2 is copied over to it? No, an assignment operator will need to deal with releasing whatever resources m1 may hold before performing an assignment. The destructor will be called only when m1 is about to go out of scope. No, once an object which is allocated on the stack is constructed it isn't deconstructed until it either goes out of scope or you call its destructor explicitly (which you should

How do lifetime bounds on structs work in Rust?

放肆的年华 提交于 2019-12-05 17:29:06
There was some discussion of this in IRC yesterday, which left me feeling vaguely dissatisfied. The question was: How do you define a lifetime on a struct to restrict its contents to only things that live as long as 'itself'. i.e. a 'self sort of thing. My initial reaction was: you can't. If you create a struct Foo<'a>, the lifetime associated with it is inferred from the references it contains; unless the struct contains a reference to itself (impossible), you can't have this sort of 'self lifetime. There was a bunch of chatter about it, and I ended up writing this playpen as a result: #

How do I fix a missing lifetime specifier?

丶灬走出姿态 提交于 2019-12-05 17:20:42
I have a very simple method. The first argument takes in vector components ("A", 5, 0) and I will compare this to every element of another vector to see if they have the same ( _ , 5 , _) and then print out the found element's string. Comparing ("A", 5, 0 ) and ("Q", 5, 2) should print out Q. fn is_same_space(x: &str, y1: i32, p: i32, vector: &Vec<(&str, i32, i32)>) -> (&str) { let mut foundString = ""; for i in 0..vector.len() { if y1 == vector[i].1 { foundString = vector[i].0; } } foundString } However, I get this error error[E0106]: missing lifetime specifier --> src/main.rs:1:80 | 1 | fn

Mysterious lifetime issue while implementing trait for dyn object

↘锁芯ラ 提交于 2019-12-05 10:14:23
问题 Consider the following toy example: use std::cmp::Ordering; pub trait SimpleOrder { fn key(&self) -> u32; } impl PartialOrd for dyn SimpleOrder { fn partial_cmp(&self, other: &dyn SimpleOrder) -> Option<Ordering> { Some(self.cmp(other)) } } impl Ord for dyn SimpleOrder { fn cmp(&self, other: &dyn SimpleOrder) -> Ordering { self.key().cmp(&other.key()) } } impl PartialEq for dyn SimpleOrder { fn eq(&self, other: &dyn SimpleOrder) -> bool { self.key() == other.key() } } impl Eq for SimpleOrder

Lifetimes and references to objects containing references

早过忘川 提交于 2019-12-05 06:29:28
Let's say I have a struct with a reference in it, and another struct with a reference to that struct, something like this: struct Image<'a> { pixel_data: &'a mut Vec<u8>, size: (i32, i32), } struct SubImage<'a> { image: &'a mut Image<'a>, offset: (i32, i32), size: (i32, i32), } The structs have nearly identical interfaces, the difference being that SubImage adjusts position parameters based on its offset before forwarding to the corresponding functions of the contained Image reference. I would like these structs to be mostly interchangeable, but I can't seem to figure out how to get lifetimes

Why does Rust disallow mutable aliasing?

此生再无相见时 提交于 2019-12-05 05:49:30
Rust disallows this kind of code because it is unsafe: fn main() { let mut i = 42; let ref_to_i_1 = unsafe { &mut *(&mut i as *mut i32) }; let ref_to_i_2 = unsafe { &mut *(&mut i as *mut i32) }; *ref_to_i_1 = 1; *ref_to_i_2 = 2; } How can I do do something bad ( e.g. segmentation fault, undefined behavior, etc.) with multiple mutable references to the same thing? The only possible issues I can see come from the lifetime of the data. Here, if i is alive, each mutable reference to it should be ok. I can see how there might be problems when threads are introduced, but why is it prevented even if

Return lazy iterator that depends on data allocated within the function

☆樱花仙子☆ 提交于 2019-12-05 05:00:15
I am new to Rust and reading The Rust Programming Language , and in the Error Handling section there is a "case study" describing a program to read data from a CSV file using the csv and rustc-serialize libraries (using getopts for argument parsing). The author writes a function search that steps through the rows of the csv file using a csv::Reader object and collect those entries whose 'city' field match a specified value into a vector and returns it. I've taken a slightly different approach than the author, but this should not affect my question. My (working) function looks like this: extern

Default mutable value from HashMap

半腔热情 提交于 2019-12-05 02:17:01
Suppose I have a HashMap and I want to get a mutable reference to an entry, or if that entry does not exist I want a mutable reference to a new object, how can I do it? I've tried using unwrap_or() , something like this: fn foo() { let mut map: HashMap<&str, Vec<&str>> = HashMap::new(); let mut ref = map.get_mut("whatever").unwrap_or( &mut Vec::<&str>::new() ); // Modify ref. } But that doesn't work because the lifetime of the Vec isn't long enough. Is there any way to tell Rust that I want the returned Vec to have the same lifetime as foo() ? I mean there is this obvious solution but I feel

Why do the lifetimes on a trait object passed as an argument require Higher Ranked Trait Bounds but a struct doesn't?

老子叫甜甜 提交于 2019-12-05 01:29:32
How are lifetimes handled when there is a trait object passed to a function? struct Planet<T> { i: T, } trait Spinner<T> { fn spin(&self, value: T); } impl<T> Spinner<T> for Planet<T> { fn spin(&self, value: T) {} } // foo2 fails: Due to lifetime of local variable being less than 'a fn foo2<'a>(t: &'a Spinner<&'a i32>) { let x: i32 = 10; t.spin(&x); } // foo1 passes: But here also the lifetime of local variable is less than 'a? fn foo1<'a>(t: &'a Planet<&'a i32>) { let x: i32 = 10; t.spin(&x); } ( Playground ) This code results in this error: error[E0597]: `x` does not live long enough --> src