borrow-checker

How to return a pointer to owned value that “does not live long enough”?

▼魔方 西西 提交于 2019-12-12 03:27:12
问题 I have the following code: struct Node { id: uint } struct Graph { nodes: Vec<Node> } impl Graph { fn new() -> Graph { return Graph { nodes: Vec::new() }; } fn create_node(&mut self) -> &Node { let index = self.nodes.len(); let node = Node { id: index }; self.nodes.push(node); // return &node; // error: `node` does not live long enough return &self.nodes[index]; // ...but this work fine } } The idea is that the graph creates a new node and "lends" it to someone who calls the method. But I can

How does multiple mutable reference prevention work in Rust?

流过昼夜 提交于 2019-12-11 19:12:57
问题 Why it is allowed to do something like this: fn main() { let mut w = MyStruct; w.fun1(); } struct MyStruct; impl MyStruct { fn fun1(&mut self) { self.fun2(); } fn fun2(&mut self) { println!("Hello world 2"); } } In the above code fun1() gets mut MyStruct and calls fun2() also with mut MyStruct . Is it double mutable reference in one scope? 回答1: This is allowed because the borrow checker can conclude there is only one mutable reference being accessed during execution. While fun2 is running, no

If let borrow stays after return even with #![feature(nll)] [duplicate]

独自空忆成欢 提交于 2019-12-11 13:35:28
问题 This question already has answers here : Double mutable borrow error in a loop happens even with NLL on (2 answers) Closed last year . I was working on a big file but this is a small toy example that causes the same issue. Sorry if the example itself makes no sense. #![feature(nll)] struct S(i32); impl S { fn foo(&mut self) -> Option<&i32> { if let Some(val) = self.bar() { return Some(val); } let y = &mut self.0; None } fn bar(&mut self) -> Option<&i32> { None } } fn main() { S(0).foo(); }

How can I pattern match a tuple containing a &mut enum and use the enum in one match arm and a recursive call in another?

∥☆過路亽.° 提交于 2019-12-11 09:02:58
问题 How can the code below be made to compile? It seems perfectly safe, but I can't convince the compiler that it is. The version matching *self gives the error: error[E0507]: cannot move out of borrowed content --> src/main.rs:8:16 | 8 | match (*self, y) { | ^^^^^ cannot move out of borrowed content The version matching self gives: error[E0382]: use of moved value: `*self` --> src/main.rs:17:26 | 8 | match (self, y) { | ---- value moved here ... 17 | (*a * b, self) | ^^^^ value used here after

Returning reference from RefCell [duplicate]

旧巷老猫 提交于 2019-12-11 08:40:02
问题 This question already has answers here : How do I borrow a RefCell<HashMap>, find a key, and return a reference to the result? [duplicate] (1 answer) How do I return a reference to something inside a RefCell without breaking encapsulation? (3 answers) Closed last year . Why does this program not compile use std::cell::RefCell; struct S { field: RefCell<String>, } impl S { fn take_ref(&self) -> &str { &self.field.borrow() } } fn main() { let s = S { field: RefCell::new("abc".to_string()), }; }

How to pass `Option<&mut …>` to multiple function calls without causing move errors?

一笑奈何 提交于 2019-12-11 05:58:24
问题 Since it's possible to pass a mutable reference to a vector around (without causing moves), how can an Option<reference> be passed to functions multiple times without causing borrow checking errors? This simple example just shows what happens when an Option<&mut Vec<usize>> is passed multiple times to a function: fn maybe_push(mut v_option: Option<&mut Vec<usize>>) -> usize { let mut c = 0; if let Some(ref mut v) = v_option.as_mut() { for i in 0..10 { v.push(i); c += i; } } return c; } fn

Double mutable borrow error in a loop happens even with NLL on

余生长醉 提交于 2019-12-11 03:59:39
问题 Suppose I have several structures like in the following example, and in the next() method I need to pull the next event using a user-provided buffer, but if this event is a comment, and ignore comments flag is set to true, I need to pull the next event again: struct Parser { ignore_comments: bool, } enum XmlEvent<'buf> { Comment(&'buf str), Other(&'buf str), } impl Parser { fn next<'buf>(&mut self, buffer: &'buf mut String) -> XmlEvent<'buf> { let result = loop { buffer.clear(); let temp

Am I incorrectly implementing IntoIterator for a reference to a LazyList implementation or is this a Rust bug?

泪湿孤枕 提交于 2019-12-10 19:55:35
问题 In implementing a version of a LazyList (an immutable lazily-computed memoized singly-linked list, much as Haskell lists), I have run into a problem of implementing IntoIterator in that the code does not drop the reference when I think it should. The following code has been simplified so as just to show the problem; thus, is not generic and does not include all of the methods not related to implementing IntoIterator : use std::cell::UnsafeCell; use std::mem::replace; use std::rc::Rc; // only

How to interpret immutable references to mutable types in Rust?

五迷三道 提交于 2019-12-10 18:48:07
问题 It seems that I cannot mutate anything if there is any immutable reference in my chain of dereferencing. A sample: fn main() { let mut x = 42; let y: &mut i32 = &mut x; // first layer let z: &&mut i32 = &y; // second layer **z = 100; // Attempt to change `x`, gives compiler error. println!("Value is: {}", z); } I'm getting the compiler error: error[E0594]: cannot assign to `**z` which is behind a `&` reference --> src/main.rs:5:5 | 4 | let z: &&mut i32 = &y; // second layer | -- help:

What does “borrowed data cannot be stored outside of its closure” mean?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:39:04
问题 When compiling the following code: fn main() { let mut fields = Vec::new(); let pusher = &mut |a: &str| { fields.push(a); }; } The compiler gives me the following error: error: borrowed data cannot be stored outside of its closure --> src/main.rs:4:21 | 3 | let pusher = &mut |a: &str| { | ------ --------- ...because it cannot outlive this closure | | | borrowed data cannot be stored into here... 4 | fields.push(a); | ^ cannot be stored outside of its closure What does this error mean, and how