borrow-checker

Rust lifetime issue in loop

耗尽温柔 提交于 2019-12-23 17:10:20
问题 How to get this example to compile without array copying or multiple calls to b() per iteration — b() has to perform some expensive parsing? This is not the full code that I wrote, but it illustrates the problem I had. Here, Test is attempting to perform some kind of streaming parsing work. c() is the parsing function, it returns Some when parsing was successful. b() is a function that attempts to read more data from the stream when c() can not parse using the available data yet. The returned

Mutable borrow in loop [duplicate]

本小妞迷上赌 提交于 2019-12-23 12:00:51
问题 This question already has answers here : Linking the lifetimes of self and a reference in method (1 answer) Cannot borrow as mutable more than once at a time in one code - but can in another very similar (2 answers) Closed 2 years ago . I am trying to get a mutable borrow inside a loop, and I cannot get it to work. I've tried all the possible guards, raw pointers, everything. struct Test<'a> { a: &'a str, } impl<'a> Test<'a> { pub fn new() -> Self { Test { a: &mut "test" } } pub fn dostuff(&

HashMap key does not live long enough

99封情书 提交于 2019-12-23 10:06:32
问题 I'm trying to use a HashMap<String, &Trait> but I have an error message I don't understand. Here's the code (playground): use std::collections::HashMap; trait Trait {} struct Struct; impl Trait for Struct {} fn main() { let mut map: HashMap<String, &Trait> = HashMap::new(); let s = Struct; map.insert("key".to_string(), &s); } Here's the error I'm getting: error[E0597]: `s` does not live long enough --> src/main.rs:12:36 | 12 | map.insert("key".to_string(), &s); | ^ borrowed value does not

How to get mutable struct from HashMap?

帅比萌擦擦* 提交于 2019-12-22 12:41:25
问题 I have a hashmap for all my states, which is a HashMap<String, Rc<State>> , and I want to call the current state's member fn init(&mut self) . But I'm getting an error with the following code: ... if let Some(state) = self.states.get_mut(state_id) { (*state).init(); } ... Here's the error: src/main.rs:70:25: 70:33 error: cannot borrow immutable borrowed content as mutable src/main.rs:70 (*state).shutdown();` afaict from the documentation, the problem is that get_mut returns a mutable

How to get mutable struct from HashMap?

折月煮酒 提交于 2019-12-22 12:40:56
问题 I have a hashmap for all my states, which is a HashMap<String, Rc<State>> , and I want to call the current state's member fn init(&mut self) . But I'm getting an error with the following code: ... if let Some(state) = self.states.get_mut(state_id) { (*state).init(); } ... Here's the error: src/main.rs:70:25: 70:33 error: cannot borrow immutable borrowed content as mutable src/main.rs:70 (*state).shutdown();` afaict from the documentation, the problem is that get_mut returns a mutable

Tree traversal in Rust vs Borrow Checker

我与影子孤独终老i 提交于 2019-12-22 05:52:36
问题 I'm attempting to implement a tree structure in Rust, traverse it, and modify it, and I'm running into trouble with the borrow checker. My setup is more or less the following: #![feature(slicing_syntax)] use std::collections::HashMap; #[deriving(PartialEq, Eq, Hash)] struct Id { id: int, // let’s pretend it’s that } struct Node { children: HashMap<Id, Box<Node>>, decoration: String, // other fields } struct Tree { root: Box<Node> } impl Tree { /// Traverse the nodes along the specified path.

Changing a node in a tree in Rust

試著忘記壹切 提交于 2019-12-19 21:25:59
问题 I'm trying to write a function that, given a tree structure, returns a copy of that tree but with a node changed at a particular index. Here is what I have so far: #[derive(Clone)] pub enum Node { Value(u32), Branch(u32, Box<Node>, Box<Node>), } fn main() { let root = Node::Branch(1, Box::new(Node::Value(2)), Box::new(Node::Value(3))); zero_node(&root, 2); } pub fn zero_node (tree: &Node, node_index: u8) -> Node { let mut new_tree = tree.clone(); fn zero_rec (node : &mut Node, node_count : u8

Borrow checker on parent-child relation

送分小仙女□ 提交于 2019-12-19 09:52:36
问题 I have the code below producing the error message marked in its comments. I think I understand the message: I want to borrow parent two times: once for finding its child, and once as an argument to the child (and the mutable/immutable words in the error are not relevant). I have to prove that Child doesn't disappear when it modifies Parent . But I don't know how to do this. I could Rc<Child> everything but that seams wasteful, so I hope adding some lifetimes would do the trick. struct Parent

Rust borrow checker: cannot borrow as mutable in array index

一个人想着一个人 提交于 2019-12-19 05:18:22
问题 I'm prettye sure that this question has come up at some point, but sadly I haven't been able to find an explanation. I'm wondering what the error "cannot borrow as mutable while also borrowed as immutable" means in the following case: let mut v: Vec<usize> = vec![1,2,3,4,5]; v[v[1]] = 999; After some digging I found that indexing is implemented via the traits Index and IndexMut and that v[1] is syntactiv sugar for *v.index(1) . Equipped with this knowledge I tried to run the following code:

Struct that owns some data and a reference to the data [duplicate]

淺唱寂寞╮ 提交于 2019-12-19 02:20:22
问题 This question already has an answer here : How to initialize struct fields which reference each other (1 answer) Closed 4 years ago . Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj { let data = compute(); Obj { original: data, processed: AnotherObj { reference: &data } } } Is it possible to express this in Rust's terms? Here I'd like Obj , AnotherObj and data to have