borrow-checker

What are the options to end a mutable borrow in Rust?

折月煮酒 提交于 2019-11-26 03:45:56
问题 I\'m strugglin\' with the borrow checker - wonder o\' wonder. While I found a solution by adding a block, I am curious if there are other ways to end a mutable borrow so the next statement can access a binding afterwards. Here\'s what I did so far: let mut canvas: Canvas = Canvas { width: 5, height: 5, array: vec![\'x\'; 5*5], }; { let mut renderer: CanvasRenderer = CanvasRenderer::new(&mut canvas); renderer.render_point(\'x\', 3, 3); } println!(\"The Value in the array is: {}\", canvas.array

Why can I return a reference to a local literal but not a variable?

浪子不回头ぞ 提交于 2019-11-26 03:20:35
问题 Why does this code compile? fn get_iter() -> impl Iterator<Item = i32> { [1, 2, 3].iter().map(|&i| i) } fn main() { let _it = get_iter(); } [1, 2, 3] is a local variable and iter() borrows it. This code should not compile because the returned value holds a reference to a local variable. 回答1: In your example, [1, 2, 3] is not treated as local variable, but as static one! Let's take a look at this code: fn foo() -> &'static [i32] { &[1, 2, 3] } This works! Some time ago, RFC 1414: Rvalue Static

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

旧巷老猫 提交于 2019-11-26 02:58:49
问题 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

Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it&#39;s in?

感情迁移 提交于 2019-11-26 00:08:59
问题 I\'ve got a persistent compile error where Rust complains that I have an immutable borrow while I\'m trying to mutably borrow, but the immutable borrow is from another scope, and I\'m not bringing anything across from it. I have some code that checks for a value in a map, and if it\'s present, returns it, otherwise it needs to mutate the map in various ways. The problem is that I can\'t seem to find a way to get Rust let me do both, even though the two operations are totally separate. Here\'s

Why can&#39;t I store a value and a reference to that value in the same struct?

非 Y 不嫁゛ 提交于 2019-11-25 23:55:56
问题 I have a value and I want to store that value and a reference to something inside that value in my own type: struct Thing { count: u32, } struct Combined<\'a>(Thing, &\'a u32); fn make_combined<\'a>() -> Combined<\'a> { let thing = Thing { count: 42 }; Combined(thing, &thing.count) } Sometimes, I have a value and I want to store that value and a reference to that value in the same structure: struct Combined<\'a>(Thing, &\'a Thing); fn make_combined<\'a>() -> Combined<\'a> { let thing = Thing:

Cannot move out of borrowed content / cannot move out of behind a shared reference

守給你的承諾、 提交于 2019-11-25 23:49:38
问题 I don\'t understand the error cannot move out of borrowed content . I have received it many times and I have always solved it, but I\'ve never understood why. For example: for line in self.xslg_file.iter() { self.buffer.clear(); for current_char in line.into_bytes().iter() { self.buffer.push(*current_char as char); } println!(\"{}\", line); } produces the error: error[E0507]: cannot move out of borrowed content --> src/main.rs:31:33 | 31 | for current_char in line.into_bytes().iter() { | ^^^^