borrow-checker

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

How to resolve “creates a temporary variable which is freed while still in use”?

心不动则不痛 提交于 2020-12-13 11:32:31
问题 I am trying to implement Debug for my custom List<T> using a while loop. use std::cell::RefCell; use std::rc::Rc; type Link<T> = Option<Rc<RefCell<Node<T>>>>; struct Node<T> { val: T, next: Link<T>, } struct List<T> { head: Link<T>, tail: Link<T>, len: usize, } use std::fmt; impl<T: fmt::Debug> fmt::Debug for List<T> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let mut temp = &self.head; while let Some(r) = temp { write!(f, "{:?} -> ", r.borrow().val); temp = &r.borrow().next;

Why are multiple mutable borrows possible in the same scope?

好久不见. 提交于 2020-12-09 04:44:25
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Why are multiple mutable borrows possible in the same scope?

烂漫一生 提交于 2020-12-09 04:42:10
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Why are multiple mutable borrows possible in the same scope?

落爺英雄遲暮 提交于 2020-12-09 04:42:09
问题 I wrote this code that borrows a mutable variable more than once and compiles without any error, but according to The Rust Programming Language this should not compile: fn main() { let mut s = String::from("hello"); println!("{}", s); test_three(&mut s); println!("{}", s); test_three(&mut s); println!("{}", s); } fn test_three(st: &mut String) { st.push('f'); } (playground) Is this a bug or there is new feature in Rust? 回答1: Nothing weird happens here; the mutable borrow becomes void every

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

人盡茶涼 提交于 2020-12-08 16:19:07
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

喜夏-厌秋 提交于 2020-12-08 16:18:06
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

醉酒当歌 提交于 2020-12-08 16:15:54
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below

Is it safe to logically split a borrow to work around a limitation of the NLL-enabled borrow-checker?

旧城冷巷雨未停 提交于 2020-12-08 16:14:30
问题 The following code involves a very subtle bit of borrow checker dodging. The code itself describes the reasoning. The questions: Is this actually safe? Is this the recommended way to express the unsafe operations performed? Should I use pointers instead? Will the new Polonius borrow checker be able to reason about patterns like this? /// Insert a new data element at a given key. pub fn insert<'a, K: Eq, V>(this: &'a mut Vec<(K, V)>, key: K, val: V) -> &'a mut V { // Safety: As indicated below