borrow-checker

Why does refactoring by extracting a method trigger a borrow checker error?

ぐ巨炮叔叔 提交于 2019-11-26 14:53:44
问题 The architecture of my networking application can be stripped down to the following: use std::collections::HashMap; /// Represents remote user. Usually has fields, /// but we omit them for the sake of example. struct User; impl User { /// Send data to remote user. fn send(&mut self, data: &str) { println!("Sending data to user: \"{}\"", data); } } /// A service that handles user data. /// Usually has non-trivial internal state, but we omit it here. struct UserHandler { users: HashMap<i32,

Mutably borrow one struct field while borrowing another in a closure

允我心安 提交于 2019-11-26 13:52:05
I have a struct containing two fields and I want to modify one field (mutable borrow) using another field (immutable borrow), but I get an error from the borrow checker. For instance, the following code: struct Struct { field1: Vec<i32>, field2: Vec<i32>, } fn main() { let mut strct = Struct { field1: vec![1, 2, 3], field2: vec![2, 3, 4], }; strct.field1.retain(|v| !strct.field2.contains(v)); println!("{:?}", strct.field1); } gives the following error: error[E0502]: cannot borrow `strct.field1` as mutable because it is also borrowed as immutable --> src/main.rs:12:5 | 12 | strct.field1.retain(

Cannot borrow as immutable because it is also borrowed as mutable in function arguments

穿精又带淫゛_ 提交于 2019-11-26 11:54:11
What is going on here ( playground )? struct Number { num: i32 } impl Number { fn set(&mut self, new_num: i32) { self.num = new_num; } fn get(&self) -> i32 { self.num } } fn main() { let mut n = Number{ num: 0 }; n.set(n.get() + 1); } Gives this error: error[E0502]: cannot borrow `n` as immutable because it is also borrowed as mutable --> <anon>:17:11 | 17 | n.set(n.get() + 1); | - ^ - mutable borrow ends here | | | | | immutable borrow occurs here | mutable borrow occurs here However if you simply change the code to this it works: fn main() { let mut n = Number{ num: 0 }; let tmp = n.get() +

“borrowed value does not live long enough” when using the builder pattern

眉间皱痕 提交于 2019-11-26 10:03:23
问题 I have the following code: pub struct Canvas<\'a> { width: isize, height: isize, color: Color, surface: Surface, texture: Texture, renderer: &\'a Renderer, } impl<\'a> Canvas<\'a> { pub fn new(width: isize, height: isize, renderer: &\'a Renderer) -> Canvas<\'a> { let color = Color::RGB(0, 30, 0); let mut surface = core::create_surface(width, height); let texture = Canvas::gen_texture(&mut surface, width, height, color, renderer); Canvas { width: width, height: height, color: color, surface:

How do I move out of a struct field that is an Option?

坚强是说给别人听的谎言 提交于 2019-11-26 10:03:16
问题 I want to collect changes to a struct and apply them all at once. The basic outline looks like this: enum SomeEnum { Foo, Bar, } struct SomeStruct { attrib: SomeEnum, next_attrib: Option<SomeEnum>, } impl SomeStruct { pub fn apply_changes(&mut self) { if let Some(se) = self.next_attrib { self.attrib = se; } self.next_attrib = None; } } which yields the following compiler error: error[E0507]: cannot move out of borrowed content --> src/lib.rs:13:27 | 13 | if let Some(se) = self.next_attrib { |

How to update-or-insert on a Vec?

梦想与她 提交于 2019-11-26 07:49:46
问题 I\'m writing a data structure in Rust. It contains a Vec of key-value pairs. When inserting into the structure, I need to find a matching key and update both the key and the value (which is actually a child pointer). The code looks a bit like this, where pivots is a ref mut to Vec<Pivot> and Pivot is just a struct with two fields: match pivots.iter_mut().find(|ref p| key <= p.min_key) { // first mutable borrow Some(ref mut pivot) => { // If there is one, insert into it and update the pivot

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

删除回忆录丶 提交于 2019-11-26 07:49:06
问题 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

Passing mutable self reference to method of owned object

China☆狼群 提交于 2019-11-26 05:57:07
问题 The following is a simple simulation with a field which is a rectangular area with two balls bouncing around in it. The Field struct has an update method, which calls update on each of the balls. The balls, in their update method, need to move around based on their velocity. But they also need to react to each other, as well as the boundaries of the field.: fn main() { let mut field = Field::new(Vector2d { x: 100, y: 100 }); field.update(); } #[derive(Copy, Clone)] struct Vector2d { x: i32, y

Cannot move out of borrowed content when trying to transfer ownership

佐手、 提交于 2019-11-26 05:37:51
问题 I\'m writing a linked list to wrap my head around Rust lifetimes, ownership and references. I have the following code: pub struct LinkedList { head: Option<Box<LinkedListNode>>, } pub struct LinkedListNode { next: Option<Box<LinkedListNode>>, } impl LinkedList { pub fn new() -> LinkedList { LinkedList { head: None } } pub fn prepend_value(&mut self) { let mut new_node = LinkedListNode { next: None }; match self.head { Some(ref head) => new_node.next = Some(*head), None => new_node.next = None

Mutably borrow one struct field while borrowing another in a closure

守給你的承諾、 提交于 2019-11-26 04:28:55
问题 I have a struct containing two fields and I want to modify one field (mutable borrow) using another field (immutable borrow), but I get an error from the borrow checker. For instance, the following code: struct Struct { field1: Vec<i32>, field2: Vec<i32>, } fn main() { let mut strct = Struct { field1: vec![1, 2, 3], field2: vec![2, 3, 4], }; strct.field1.retain(|v| !strct.field2.contains(v)); println!(\"{:?}\", strct.field1); } gives the following error: error[E0502]: cannot borrow `strct