borrow-checker Questions

4

Solved

Consider the following example from the Book: fn main() { let string1 = String::from("abcd"); let string2 = "xyz"; let result = longest(string1.as_str(), string2); println!...
Discrepant asked 2/6, 2021 at 9:32

1

tldr;> Given a trait function that takes a generic callback argument which returns an associated type, the compiler complains that the associated type may borrow arguments from the callback func...
Quadrangular asked 14/8, 2023 at 1:25

2

I have this code, for which borrow checker shows error: https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=55d050b6f25410ce0e17ef9e844b048d fn f1(v: &str) { } ...
Titfer asked 12/9, 2020 at 1:31

2

Solved

I have multiple threads doing a computation and want to collect the results into a pre-allocated vector. To turn off the borrow checker, I wrote the function: fn set_unsync(vec: &Vec<usize&g...
Negro asked 10/10, 2022 at 20:33

1

The Rust book provides the following code to illustrate a valid function definition where explicit lifetimes are required: fn longest<'a>(x: &'a str, y: &'a str) -> &'a str { ...
Hydrochloride asked 2/5 at 17:49

1

Solved

I'd like to make the following Padded type into an iterator transformer: enum Step<T> { Before(T), During(T), After } struct Padded<T> { step: Step<T> } (Note that in my rea...
Taciturnity asked 8/1 at 13:56

2

I want to do something like this: fn some_fn() { let mut my_map = HashMap::from([ (1, "1.0".to_string()), (2, "2.0".to_string()), ]); let key = 3; let res = match my_map...
Brahman asked 10/6, 2022 at 12:57

2

I get an unexpected error from this Rust code: struct Container<'a> { x: &'a i32, } trait Reply {} impl Reply for i32 {} fn json<T>(_val: &T) -> impl Reply { 3 } fn f()...
Skillless asked 17/9, 2019 at 14:5

2

Solved

Brief: I'm new to Rust so I decided to practice by implementing a double linked list. For debugging purpose, I implemented the get() method, but I failed to copy the value out from a Rc<RefCell&...
Inveigle asked 24/8, 2023 at 13:19

2

Solved

I'm attempting to store a reference to an element of a mutable vector to use later. However, once I mutate the vector, I can no longer use the stored reference. I understand that this is because bo...
Minter asked 16/2, 2021 at 5:44

3

Solved

To illustrate the necessity of Rc<T>, the Book presents the following snippet (spoiler: it won't compile) to show that we cannot enable multiple ownership without Rc<T>. enum List { Co...
Retiarius asked 29/5, 2021 at 4:10

2

Solved

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...
Coquillage asked 26/1, 2015 at 21:4

1

Solved

Let's say we have declared an immutable (not let mut) RwLock instance like: let value = RwLock::new(0); Because the value is immutable, I expected that I can not change the inner value of the RwLo...
Impeditive asked 19/3, 2023 at 22:39

1

Solved

so I'm relatively new in Rust and I was trying to get something similair to a std::shared_ptr in C++. I decided to go with the Rc<RefCell> pattern. I'm trying to get and modify the value of R...
Woolard asked 19/1, 2023 at 16:30

1

I'm currently working in wgpu in order to reap the efficiency benefits over OpenGl. A common pattern in my previous graphics code is to draw many meshes with separate buffers, separate uniform data...
Buckhound asked 30/8, 2020 at 23:36

0

I was learning Rust and all the references and borrowing stuff when I came across this weird case: fn main() { let mut n = 42u32; let ref1 = &mut n; let ref2 = ref1; // line 5 ref...
Pennywise asked 7/12, 2022 at 4:15

2

Solved

Why is the following invalid and what should I do instead to make it work? struct Foo; impl Foo { fn mutable1(&mut self) -> Result<(), &str> { Ok(()) } fn mutable2(&mut ...
Stomachache asked 9/10, 2019 at 0:23

3

Solved

I have a MainStruct which owns an instance of HelperStruct. I want to make a method that calls helper's method. But the helper's method really needs (immutable) access to MainStruct struct MainStru...
Moreno asked 29/10, 2022 at 18:17

1

Solved

Interested why does set method defined on Cell, on the last line explicitly drops old value. Shouldn't it be implicitly dropped (memory freed) anyways when the function returns? use std::mem; use s...
Achromatic asked 19/10, 2022 at 10:19

2

Solved

I have the following code: struct Solver<'a> { guesses: Vec<&'a str>, } impl<'a> Solver<'a> { fn register_guess(&mut self, guess: &'a str) { self.guesses.pus...
Feininger asked 9/10, 2022 at 22:53

1

Solved

I am trying to implement a binary tree. I want the node data to be separate, because there are many different ways in which this can be implemented, while algorithms on the tree should be generic a...
Vitovitoria asked 26/8, 2022 at 2:50

1

Solved

I'm attempting to build a sort of HTTP web server as a learning exercise and I'm having trouble trying to make one of my types iterable using a for loop (by implementing IntoIterator). So far I've ...
Handedness asked 23/8, 2022 at 18:19

3

Solved

Since it's possible to pass a mutable reference to a vector around (without causing moves), how can an Option<reference> be passed to functions multiple times without causing borrow checking ...
Popsicle asked 17/11, 2016 at 15:13

4

Solved

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_...
Humbug asked 30/8, 2015 at 19:6

4

Solved

How can I call a method in closure? get_access_token method can set new access token based on self.get_base_url(): fn fetch_access_token(_base_url: &String) -> String { String::new() } fn...
Nagari asked 10/2, 2018 at 6:11

© 2022 - 2024 — McMap. All rights reserved.