borrow-checker

How to tell the borrow checker that a cleared Vec contains no borrows? [duplicate]

瘦欲@ 提交于 2020-05-14 18:07:50
问题 This question already has answers here : Borrow checker doesn't realize that `clear` drops reference to local variable (6 answers) Closed 22 days ago . I'm processing a massive TSV (tab separated values) file and want to do this as efficiently as possible. To that end, I thought I'd prevent allocation of a new Vec for every line by pre-allocating it before the loop: let mut line = String::new(); let mut fields = Vec::with_capacity(headers.len()); while reader.read_line(&mut line)? > 0 {

How to avoid multiple mutable borrows of a vector when inserting a value if the vector is empty?

こ雲淡風輕ζ 提交于 2020-03-01 05:17:11
问题 In this github discussion you find this code that draws the ire of the borrow checker: fn main() { let mut vec = vec!(); match vec.first() { None => vec.push(5), Some(v) => unreachable!(), } } I understand why having a mutation while immutable borrows are outstanding is problematic. I assumed that a solution was to explicitly only have one borrow (a mutable one) but it still resulted in my having two borrows, an immutable borrow and then a mutable borrow: fn main() { let mut vec: Vec<i32> =

How to avoid multiple mutable borrows of a vector when inserting a value if the vector is empty?

两盒软妹~` 提交于 2020-03-01 05:16:03
问题 In this github discussion you find this code that draws the ire of the borrow checker: fn main() { let mut vec = vec!(); match vec.first() { None => vec.push(5), Some(v) => unreachable!(), } } I understand why having a mutation while immutable borrows are outstanding is problematic. I assumed that a solution was to explicitly only have one borrow (a mutable one) but it still resulted in my having two borrows, an immutable borrow and then a mutable borrow: fn main() { let mut vec: Vec<i32> =

Can't build a rusqlite transaction inside loop: use of moved value and cannot borrow as mutable more than once at a time

送分小仙女□ 提交于 2020-02-26 03:35:10
问题 In order to speed up insertions into a SQLite DB using rusqlite, I want to build a transaction inside a for loop and only commit every N iterations. The following code compiles but it builds a single transaction and commits it all in one go: use rusqlite::{Connection, Result, NO_PARAMS}; fn main() -> Result<()> { let mut conn = Connection::open_in_memory()?; conn.execute( "CREATE TABLE entry ( id INTEGER PRIMARY KEY, data INTEGER )", NO_PARAMS, )?; let tx = conn.transaction()?; for i in 0..20

“cannot move out borrowed content” when assigning a variable from a struct field

强颜欢笑 提交于 2020-02-15 08:28:12
问题 I'm learning Rust and I'm fighting against the borrow checker. I have a basic Point structure. I have a scale function that modifies all the coordinates of the point. I would like to call this method from another method named convert : struct AngleUnit; struct Point { x: f32, y: f32, z: f32, unit: AngleUnit, } fn factor(_from: AngleUnit, _to: AngleUnit) -> f32 { 1.0 } impl Point { pub fn new(x: f32, y: f32, z: f32, unit: AngleUnit) -> Point { Point { x, y, z, unit } } fn scale(&mut self,

How to borrow the T from a RefCell<T> as a reference?

坚强是说给别人听的谎言 提交于 2020-02-03 15:25:50
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

How to borrow the T from a RefCell<T> as a reference?

孤者浪人 提交于 2020-02-03 15:25:28
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

How to borrow the T from a RefCell<T> as a reference?

孤者浪人 提交于 2020-02-03 15:24:26
问题 Sometimes I have a struct containing a value which is wrapped in a RefCell , and I want to borrow the value, but I don't want to make the signature of the accessor function to depend on the internal implementation. To make it work, I need to return the reference as a Ref<T> instead of a &T . For example, if this is my struct: use std::cell::RefCell; pub struct Outer<T> { inner: RefCell<T>, } I could write an accessor like this: use std::cell::Ref; impl<T> Outer<T> { fn get_inner_ref(&self) ->

What happens on the stack when one value shadows another in Rust? [duplicate]

这一生的挚爱 提交于 2020-02-03 10:36:50
问题 This question already has answers here : Does Rust free up the memory of overwritten variables? (2 answers) In Rust, what's the difference between “shadowing” and “mutability”? (1 answer) Closed 8 months ago . I am reading Mastering Rust. There is an exercise at the end of the first chapter where sample code is provided, and the task is to fix it, iterating by using the generally quite helpful compiler error messages. I was expecting that the following was an error but it is not : for line in

Is there one-line syntax for constructing a struct that contains a reference to a temporary?

筅森魡賤 提交于 2020-01-30 02:52:12
问题 Consider the following invalid Rust code. There is one struct Foo that contains a reference to a second struct Bar : struct Foo<'a> { bar: &'a Bar, } impl<'a> Foo<'a> { fn new(bar: &'a Bar) -> Foo<'a> { Foo { bar } } } struct Bar { value: String, } impl Bar { fn empty() -> Bar { Bar { value: String::from("***"), } } } fn main() { let foo = Foo::new(&Bar::empty()); println!("{}", foo.bar.value); } The compiler does not like this: error[E0716]: temporary value dropped while borrowed --> src