borrowing

Why does re-borrowing only work on de-referenced pointers?

不打扰是莪最后的温柔 提交于 2021-02-10 14:23:28
问题 This question and code are adapted from Why does creating a mutable reference to a dereferenced mutable reference work?. The answers there explained re-borrowing but not the reasons for the conventions around it. The two calls to test below seem equivalent, why does only the first one work? fn main() { let mut string = String::new(); let ref_string = &mut string; // Compiles test(&mut *ref_string); // Doesn't compile test(&mut string); } fn test(s: &mut String) { s.push('t'); } 回答1: You are

Why does re-borrowing only work on de-referenced pointers?

女生的网名这么多〃 提交于 2021-02-10 14:20:40
问题 This question and code are adapted from Why does creating a mutable reference to a dereferenced mutable reference work?. The answers there explained re-borrowing but not the reasons for the conventions around it. The two calls to test below seem equivalent, why does only the first one work? fn main() { let mut string = String::new(); let ref_string = &mut string; // Compiles test(&mut *ref_string); // Doesn't compile test(&mut string); } fn test(s: &mut String) { s.push('t'); } 回答1: You are

Lifetime issues with a closure argument in Rust

佐手、 提交于 2021-02-08 06:53:21
问题 I'm getting an error when trying to use a closure that does exactly the same as the print function below (in ln.9) The error is the usual borrowed value does not live long enough . I've tried to replicate this in the playground but I can't. I'm certain that this is mainly because I don't really understand what's going on here so any help would be really appreciated. What I can't understand is what is the difference between calling the print function and calling the check closure. They have

How to delete item from Hashmap inside a RefCell within an RwLock-ed Struct

眉间皱痕 提交于 2021-01-29 13:11:08
问题 I have a struct: pub struct CommunityContents { pub friends: RefCell<HashMap<FriendID, FriendData>>, pub index: RefCell<HashMap<u64, BTreeMap<FriendID, FriendData>>>, pub authenticated: bool, pub age: u64, pub height: u64, } Which is protected with a RwLock with a parent struct: pub struct Community { pub community_contents: RwLock<CommunityContents>, } pub struct FriendData { pointer: Rc<Data>, } pub struct Data { pub key: Key, pub friend_ids: Vec<FriendID>, } I want to be able to modify the

Rust Inspect Iterator: cannot borrow `*` as immutable because it is also borrowed as mutable

試著忘記壹切 提交于 2021-01-28 18:27:13
问题 Why can't I push to this vector during inspect and do contains on it during skip_while ? I've implemented my own iterator for my own struct Chain like this: struct Chain { n: u32, } impl Chain { fn new(start: u32) -> Chain { Chain { n: start } } } impl Iterator for Chain { type Item = u32; fn next(&mut self) -> Option<u32> { self.n = digit_factorial_sum(self.n); Some(self.n) } } Now what I'd like to do it take while the iterator is producing unique values. So I'm inspect -ing the chain and

How to borrow a field for serialization but create it during deserialization?

自闭症网瘾萝莉.ら 提交于 2021-01-27 19:27:02
问题 I have a struct like this: #[derive(Serialize, Deserialize)] struct Thing { pub small_header: Header, pub big_body: Body, } I want to serialize this Thing to send over the network. I already have a Body available but I can't move it (imagine I am doing something with it, and every now and then I receive a command to temporarily stop what I'm doing and send over whatever data I have now) and I can't copy it (it's too big, possibly hundreds of megabytes). So I'd like Serde to just borrow the

Why can the Rust compiler break borrowing rules when using Rust 1.31?

北城以北 提交于 2021-01-02 07:17:30
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow

Why can the Rust compiler break borrowing rules when using Rust 1.31?

老子叫甜甜 提交于 2021-01-02 07:15:26
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow

Why can the Rust compiler break borrowing rules when using Rust 1.31?

爱⌒轻易说出口 提交于 2021-01-02 07:15:17
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow

Why can the Rust compiler break borrowing rules when using Rust 1.31?

风格不统一 提交于 2021-01-02 07:15:14
问题 I am studying Rust by Example and running code from the "Alias" page: struct Point { x: i32, y: i32, z: i32, } fn main() { let mut point = Point { x: 0, y: 0, z: 0 }; { let borrowed_point = &point; let another_borrow = &point; // Data can be accessed via the references and the original owner println!( "Point has coordinates: ({}, {}, {})", borrowed_point.x, another_borrow.y, point.z ); // Error! Can't borrow point as mutable because it's currently // borrowed as immutable. let mutable_borrow