lifetime

Why does Rust disallow mutable aliasing?

梦想与她 提交于 2019-12-22 04:54:06
问题 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

How can I take ownership of a Vec element and replace it with something else?

江枫思渺然 提交于 2019-12-22 03:53:08
问题 I am writing a function of the following format: fn pop(data: &mut Vec<Option<T>>) -> Option<T> { // Let the item be the current element at head let item = data[0]; // and "remove" it. data[0] = None; item } When I try to do this, I get a "cannot move out of indexed content" error, which makes sense. When I try to change it such that item is a reference, I get an error when I try to set data[0] to None , which also makes sense. Is there some way I can do what I want to do? It seems to me that

Application lifetime in ASP.NET

你说的曾经没有我的故事 提交于 2019-12-21 11:25:11
问题 This should be a simple question but I haven't managed to find the answer on google. I would like to know, in terms an idiot can understand, exactly what application lifetime means in ASP.NET (and therefore when you can expect application start and end events to run). I assumed it would be when you run and stop the app in IIS, but I've read things that suggest it's related to number of requests. 回答1: By default the lifetime starts with the first request to the app. And it ends after an idle

Lifetime of temporary bound to aggregate initialized struct member

和自甴很熟 提交于 2019-12-21 07:34:09
问题 Given the following code: class foo { }; class bar: public foo { public: ~bar() { printf("~bar()\n"); } }; class zab: public foo { public: ~zab() { printf("~zab()\n"); } }; struct foo_holder { const foo &f; }; int main() { foo_holder holder[]= { {bar()}, {zab()} }; printf("done!\n"); return 0; } the output is: ~bar() ~zab() done! C++0x has a clause that dictates this can create dangling references when used as a new initializer, but it says nothing (at least nothing I can find) about

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

主宰稳场 提交于 2019-12-21 07:33:10
问题 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)

Why doesn't my struct live long enough?

岁酱吖の 提交于 2019-12-21 05:10:15
问题 In Rust, I get the following error: <anon>:14:9: 14:17 error: `mystruct` does not live long enough <anon>:14 mystruct.update(); ^~~~~~~~ <anon>:10:5: 17:6 note: reference must be valid for the lifetime 'a as defined on the block at 10:4... <anon>:10 { <anon>:11 let initial = vec![Box::new(1), Box::new(2)]; <anon>:12 let mystruct = MyStruct { v : initial, p : &arg }; <anon>:13 <anon>:14 mystruct.update(); <anon>:15 ... <anon>:12:59: 17:6 note: ...but borrowed value is only valid for the block

Undead objects ([basic.life]/8): why is reference rebinding (and const modification) allowed?

。_饼干妹妹 提交于 2019-12-21 05:03:22
问题 The "undead" clause I call the undead clause the C++ rule that after the destruction of an object, if a new object is created at the same address, it can sometimes be considered the same object as the old one. That rule always existed in C++ but with some changes on the additional conditions. I was made to read the latest undead clause by this question. The revised conditions in Lifetime [basic.life]/8 are: (8.1) the storage for the new object exactly overlays the storage location which the

Temporary lifetime extension

≡放荡痞女 提交于 2019-12-21 03:55:06
问题 The 12.2.5 section of standard says: A temporary bound to a reference parameter in a function call (5.2.2) persists until the completion of the full expression containing the call. A temporary bound to the returned value in a function return statement (6.6.3) persists until the function exits. In all these cases, the temporaries created during the evaluation of the expression initializing the reference, except the temporary to which the reference is bound, are destroyed at the end of the full

Rust: error[E0495]: cannot infer an appropriate lifetime for autoref due to conflicting requirements

为君一笑 提交于 2019-12-20 06:14:48
问题 This is the minimal code: struct Node<T> { item: T, next: Link<T>, } type Link<T> = Option<Box<Node<T>>>; pub struct IterMut<'a, T>(&'a mut Link<T>); impl<'a, T> Iterator for IterMut<'a, T> { type Item = &'a mut T; fn next(&mut self) -> Option<Self::Item> { self.0.as_mut().map(|boxed_node| { self.0 = &mut boxed_node.next; &mut boxed_node.item }) } } As far as I understand, there should be no problem. I have done a lot of searching, but no way. error[E0495]: cannot infer an appropriate

Is there a way to use locked standard input and output in a constructor to live as long as the struct you're constructing?

妖精的绣舞 提交于 2019-12-20 05:36:48
问题 I'm building a PromptSet that can ask a series of questions in a row. For testing reasons, it allows you to pass a reader and writer instead of using stdin & stdout directly. Because stdin and stdout are the common use case, I would like to create a default "constructor" that allows the user to produce a PromptSet<StdinLock, StdoutLock> without needing any parameters. Here's the code so far: use std::io::{self, BufRead, StdinLock, StdoutLock, Write}; pub struct PromptSet<R, W> where R: