Questions tagged [borrow-checker]

The borrow checker refers to a compile time analysis of the ownership concept used by the Rust programming language. This tag should be used for related issues and errors.

Many new users to experience something we like to call ‘fighting with the borrow checker’, where the Rust compiler refuses to compile a program that the author thinks is valid. There are currently no other programming languages which have an equivalent concept.

The Rust book presents Rust’s ownership concept in three sections:

The borrow checker refers to the compile time validation of these concepts.

1381 questions
406
votes
4 answers

Why can't I store a value and a reference to that value in the same struct?

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 {…
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
195
votes
2 answers

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

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…
Peekmo
  • 2,843
  • 2
  • 19
  • 25
47
votes
1 answer

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

Why does this code compile? fn get_iter() -> impl Iterator { [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…
Boiethios
  • 38,438
  • 19
  • 134
  • 183
36
votes
1 answer

Do mutable references have move semantics?

fn main() { let mut name = String::from("Charlie"); let x = &mut name; let y = x; // x has been moved say_hello(y); say_hello(y); // but y has not been moved, it is still usable change_string(y); …
oberblastmeister
  • 864
  • 9
  • 13
36
votes
1 answer

Returning a reference from a HashMap or Vec causes a borrow to last beyond the scope it's in?

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…
Bill Fraser
  • 908
  • 7
  • 14
35
votes
1 answer

Temporary value is freed at the end of this statement

I'm trying to scrape a webpage using the Select crate: let document = Document::from_read(response).unwrap(); for node in document.find(Class("lia-list-row")) { let title = node.find(Class("page-link")).next().unwrap(); let title_text =…
mottosson
  • 3,283
  • 4
  • 35
  • 73
34
votes
2 answers

Why is Rust NLL not working for multiple borrows in the same statement?

First, I tried something like this: let mut vec = vec![0]; vec.rotate_right(vec.len()); It can't be compiled because: error[E0502]: cannot borrow `vec` as immutable because it is also borrowed as mutable I thought that the Rust borrow checker…
Direktor
  • 491
  • 2
  • 8
30
votes
1 answer

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

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…
Timmmm
  • 88,195
  • 71
  • 364
  • 509
29
votes
3 answers

Removing entries from a HashMap based on value

I've written the following code (+ demo) to remove entries from a HashMap based on value. It works, but I feel like I'm struggling against the borrow-checker with the use of: clone() to avoid two references to the same set of keys an extra let tmp…
Bosh
  • 8,138
  • 11
  • 51
  • 77
27
votes
1 answer

How can I do a mutable borrow in a for loop?

I tried: fn main() { let mut vec = [1, 2, 3]; for mut x in &vec { *x = 3; } for mut &x in &vec { x = 3; } for mut *x in &vec { x = 3; } for mut x in mut &vec { *x = 3; } for mut x in &(mut vec) { *x = 3; } } None of…
ZisIsNotZis
  • 1,570
  • 1
  • 13
  • 30
27
votes
1 answer

Cannot move out of borrowed content when trying to transfer ownership

I'm writing a linked list to wrap my head around Rust lifetimes, ownership and references. I have the following code: pub struct LinkedList { head: Option>, } pub struct LinkedListNode { next:…
Gilles
  • 5,269
  • 4
  • 34
  • 66
25
votes
3 answers

Manipulating an object from inside a loop that borrows it

I'm writing some code in Rust that connects to a remote server, and depending on the messages sent by that server, computes some statistics or executes actions based on these statistics. But this is more of a learning project for me and I've run…
Shtong
  • 1,757
  • 16
  • 30
24
votes
2 answers

Getting "temporary value dropped while borrowed" when trying to update an Option<&str> in a loop

I'm trying to implement a commonly used pattern - using the result of a previous loop iteration in the next loop iteration. For example, to implement pagination where you need to give the id of the last value on the previous page. struct Result { …
ralh
  • 2,514
  • 1
  • 13
  • 19
23
votes
2 answers

Cannot move out of value which is behind a shared reference when unwrapping

This is the code I am trying to execute: fn my_fn(arg1: &Option>) -> i32 { if arg1.is_none() { return 0; } let integer = arg1.unwrap(); *integer } fn main() { let integer = 42; …
Moebius
  • 6,242
  • 7
  • 42
  • 54
23
votes
1 answer

Struct that owns some data and a reference to the data

Construction of an object allocates data needed for lifetime of that object, but also creates another object that needs to keep references to the data: pub fn new() -> Obj { let data = compute(); Obj { original: data, …
Kornel
  • 97,764
  • 37
  • 219
  • 309
1
2 3
91 92