Questions tagged [borrow]

58 questions
5
votes
1 answer

Rust lifetime syntax when borrowing variables

New to Rust and trying to teach myself, etc. I'm stuck on a lifetime issue. The closest question I could find that was already posted was: Argument requires that _ is borrowed for 'static - how do I work round this? The small project I'm playing…
growling_egg
  • 307
  • 1
  • 9
5
votes
0 answers

"borrowed value does not live long enough" when using tokio::spawn with a future with a mutable reference

The following code does not compile bceause the compiler can not assure hashmap_of_lists would live long enough. I can not overcome this. I've tried using Arc and Mutex but then I had other issues because of the async fashion of some_func and using…
Gal Ben David
  • 410
  • 4
  • 10
5
votes
1 answer

Why is there a borrow error when no borrowing overlap is occurring?

The following code fails with a borrow error: extern crate chrono; // 0.4.6 fn main() { let mut now = chrono::Local::today(); now = std::mem::replace(&mut now, now.succ()); } The error is: error[E0502]: cannot borrow `now` as immutable…
Listerone
  • 1,381
  • 1
  • 11
  • 25
4
votes
2 answers

Why does Rust reuse memory with same value

Example code: fn main() { let mut y = &5; // 1 println!("{:p}", y); { let x = &2; // 2 println!("{:p}", x); y = x; } y = &3; // 3 println!("{:p}", y); } If third assignment contains &3 then code…
TupleCats
  • 351
  • 3
  • 15
3
votes
2 answers

Why are match statements more picky about reference types than function parameters?

In Rust, one often sees functions that take &str as a parameter. fn foo(bar: &str) { println!("{}", bar); } When calling functions like this, it is perfectly fine to pass in a String as an argument by referencing it. let bar =…
Smarwell
  • 33
  • 3
3
votes
2 answers

Borrowing errors bypassable with an intermediate variable

I am trying to implement a simple bucketed hash table in Rust (just for practice). The hash table struct is defined as: pub struct BucketedHashTable { buckets: Vec>, size: usize, } where Bucket is my simple…
German Vekhorev
  • 339
  • 6
  • 16
3
votes
1 answer

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

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…
Mike Williamson
  • 4,915
  • 14
  • 67
  • 104
2
votes
1 answer

How can I wrap the read portion of a struct which implements both Read and Write in a BufReader?

I am trying to read a line from a rustls::StreamOwned object. My first instinct was to wrap it in a BufReader, but the problem is that I need to be able to interleave my read_line() calls with write() calls, and I cannot use the underlying stream…
curious
  • 133
  • 7
2
votes
1 answer

How to create factory that dynamically creates values and returns borrows to them?

I'd like to have a struct called Factory that dynamically produces new Strings, keeps them inside itself and returns &str borrows of them that live as long as the Factory value itself. I tried to keep new values inside in a Vec but as Vec grows…
Kamil Szot
  • 17,436
  • 6
  • 62
  • 65
2
votes
2 answers

Why does calling .to_string() on a String avoid a move error? Isn't it a no-op?

The following test program would not compile: fn f1( string: String) { println!("{}", string ); } fn f2( string: String) { println!("{}", string ); } fn main() { let my_string: String = "ABCDE".to_string(); f1( my_string ); f2(…
Kalle Svensson
  • 353
  • 1
  • 10
2
votes
0 answers

Rust type annotation on left side of let pattern matching

The compiler will complain that z is moved and can not be referenced anymore: Rust playground let mut x = 100 ; let z = &mut x ; let z1 = z ; *z1 = 200 ; println!("{}", z) ; // <== compile error error[E0382]: borrow of moved value: z While…
xyanping
  • 21
  • 2
2
votes
1 answer

How does destructuring of borrowed data work at compile time?

I've been using destructuring to create references into nested data, just for practice. I created a method that uses destructuring to break apart a borrowed tuple: fn print_strings((x, y): &(String, String)) { println!("x: {}, y: {}", x,…
C-RAD
  • 1,052
  • 9
  • 18
2
votes
1 answer

Why I got "cannot borrow `arr[_]` as mutable more than once at a time"?

I have a programming assignment of creating a bubble sort using Rust. I don't really have much experience in Rust so this is a little bit hard for me: fn main() { println!("Sort numbers ascending"); let num:[i32; 10] = [4, 65, 2, -31, 0, 99,…
Tom Tom
  • 23
  • 2
2
votes
2 answers

multiple self borrows within trait default methods

I am running into issues with the borrow checker. I have a trait (Physics) which has getters (e.g. velocity) and setters (e.g. velocity_mut). It also has default methods accelerate and apply_force which uses the getters and setters. Why is it not…
dylan
  • 289
  • 2
  • 11
2
votes
3 answers

Why can I push items into a Vec I am iterating over with a while loop, but not with a for loop?

Like the rust code below: the while loop compiles and runs fine, but for iter version does not compile, due to error: error[E0502]: cannot borrow `v` as mutable because it is also borrowed as immutable --> src/main.rs:22:9 | 20 | for i in…
wtsolid
  • 41
  • 3
1
2 3 4