mutability

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 `self.x` as immutable because `*self` is also borrowed as mutable

感情迁移 提交于 2019-11-26 08:34:30
问题 First, let the code speak: #[derive(Debug)] struct Bar; #[derive(Debug)] struct Qux { baz: bool } #[derive(Debug)] struct Foo { bars: Vec<Bar>, qux: Qux, } impl Foo { fn get_qux(&mut self) -> &mut Qux { &mut self.qux } fn run(&mut self) { // 1. Fails: let mut qux = self.get_qux(); // 2. Works: // let mut qux = &mut Qux { baz: false }; // 3. Works: // let mut qux = &mut self.qux; let qux_mut = &mut qux; qux_mut.baz = true; for bar in &self.bars { println!(\"{:?}\", bar); } } } fn main() {

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

Aren&#39;t Python strings immutable? Then why does a + “ ” + b work?

依然范特西╮ 提交于 2019-11-26 03:50:20
My understanding was that Python strings are immutable. I tried the following code: a = "Dog" b = "eats" c = "treats" print a, b, c # Dog eats treats print a + " " + b + " " + c # Dog eats treats print a # Dog a = a + " " + b + " " + c print a # Dog eats treats # !!! Shouldn't Python have prevented the assignment? I am probably missing something. Any idea? First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You didn't actually mutate the string "Dog". Strings are immutable, variables can point at whatever they want. The string

Aren&#39;t Python strings immutable? Then why does a + “ ” + b work?

£可爱£侵袭症+ 提交于 2019-11-26 03:24:33
问题 My understanding was that Python strings are immutable. I tried the following code: a = \"Dog\" b = \"eats\" c = \"treats\" print a, b, c # Dog eats treats print a + \" \" + b + \" \" + c # Dog eats treats print a # Dog a = a + \" \" + b + \" \" + c print a # Dog eats treats # !!! Shouldn\'t Python have prevented the assignment? I am probably missing something. Any idea? 回答1: First a pointed to the string "Dog". Then you changed the variable a to point at a new string "Dog eats treats". You

How do I return a reference to something inside a RefCell without breaking encapsulation?

谁说胖子不能爱 提交于 2019-11-26 02:37:57
问题 I have a struct that has inner mutability. use std::cell::RefCell; struct MutableInterior { hide_me: i32, vec: Vec<i32>, } struct Foo { //although not used in this particular snippet, //the motivating problem uses interior mutability //via RefCell. interior: RefCell<MutableInterior>, } impl Foo { pub fn get_items(&self) -> &Vec<i32> { &self.interior.borrow().vec } } fn main() { let f = Foo { interior: RefCell::new(MutableInterior { vec: Vec::new(), hide_me: 2, }), }; let borrowed_f = &f; let