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.
borrow-checker
1,512
questions
0
votes
0
answers
27
views
Rust lifetimes in structs [duplicate]
Why is it not working?
struct Test<'s>(&'s mut i32);
impl<'n, 's: 'n> Test<'s> {
fn new(i: &'s mut i32) -> Self {
Self(i)
}
fn cast(&'s mut self) ...
0
votes
0
answers
49
views
How do you idiomatically DRY up rust code without upsetting the borrow checker?
Context
Coming from OOP I'm finding as I implement various traits and functions on my structs, I want to abstract bits and pieces of them out. But whenever I do, I run into borrow checker issues. I'm ...
0
votes
1
answer
37
views
A general approach to a specific type of borrowing conflict problem in Rust
When writing Rust, I often encounter a similar issue that leads to compilation errors, specifically mutable borrow conflicts (E0502). I have simplified it into the following form:
pub struct Foo{
/...
1
vote
1
answer
34
views
How to fix my code so the HashMap hmap_for_data holds the counts of items in immutable vector aux_vec_i32?
This is a simplified version of code that I written for a leetcode question. In the problem from leetcode I have an immutable input like aux_vec_i32 below and I need to return something more ...
0
votes
0
answers
53
views
Cannot borrow `p` as mutable more than once at a time [duplicate]
I'm trying to figure out exactly why I get this compile error in this very simple code;
cannot borrow p as mutable more than once at a time
as I do not store p more than once mutably; either ...
0
votes
1
answer
81
views
How can I pass a reference to a closure in Rust?
I am using wry to spawn a few web views. Each view has a on_page_load_handler. Inside the handler I need to access the web view to e.g. navigate to another website. Sadly I fail to pass the web view ...
0
votes
1
answer
104
views
How to create a self referential struct?
Context: I'm working on a very fast algorithm to solve the Advent of Code 2023 Day 8 part 2 challenge without the least-common-multiple approach (which is meant to be impossible, hence the ...
0
votes
1
answer
93
views
Can I Prove Monotonicity of Allocations to the Rust Borrow Checker
I have the following code which does not compile:
// TODO: Return Result, remove `.expect`s
fn to_blender_subfile<'a>(
filepath: &str,
transform: Transform,
visited_file_cache: &...
0
votes
2
answers
66
views
Borrow checker and &mut borrows for generic params
Why can I not borrow &mut x multiple times when using is_mut_t. But it's fine with is_mut_specific ?
#[cfg(test)]
mod test {
use std::marker::PhantomData;
#[test]
fn test_wc() {
...
1
vote
0
answers
35
views
Why does Rust allow multiple mutable borrows of struct fields but not array elements? [duplicate]
I am new to Rust (Moving from C++) and I cannot seem to wrap my head around the Rust's compile time borrowchecker for this specific case:
Using a struct
When I create a struct and mutably borrow ...
0
votes
1
answer
82
views
Why does moving a variable into a spawned thread pass the borrow checker even when not copied? Doesn't that mean it uses a pointer to the original?
I have this code but I do not understand why it compiles:
use std::thread;
use std::time::Duration;
struct State {
value: u32,
}
impl State{
pub fn new() -> State{
State{ value: 0 }...
0
votes
1
answer
71
views
Idiomatic way to write a decorator over an iterator in Rust
I am new to Rust and still fighting with the borrow-checker.
I am using this question as my general learning opportunity. So, I would appreciate an explanation that is applicable generally to similar ...
1
vote
1
answer
66
views
How to avoid a clone when passing a value to a consuming function, but needing it back in the event of an error?
Say I have a function that calls a Rust API that consumes a value. That API function returns a Result, potentially indicating an error. If the error happens, I need some information from the original ...
0
votes
1
answer
76
views
Understanding Rust Lifetimes in Mixed Mutable and Immutable References
While reading an introductory book on Rust, I was stumped by a quiz. I chose the correct answer, but I found that my reasoning differed somewhat from the answer explanation.
As shown below, we need to ...
0
votes
2
answers
78
views
How to debug without changing all the function signatures?
I'm learning Rust by reading the book and doing rustlings and watching videos; I think I understand what's happening here, but I don't understand what to do about it.
#[derive(Debug)]
enum Message {
...