More than one mut borrow question
⚓ Rust 📅 2026-02-27 👤 surdeus 👁️ 1Hi, I would like to understand why in the following example we have an error of foo_two but not foo_one. I think those functions are equivalent. What I mean is that the inner call to ".val()" has to come before the outer one during compilation, right?.
pub struct MyStruct {
pub val: usize,
}
impl MyStruct {
pub fn val(&mut self, val: usize) -> usize {
self.val + val
}
}
pub fn foo_one(bar: &mut MyStruct) {
let x = bar.val(0);
bar.val(x);
}
pub fn foo_two(bar: &mut MyStruct) {
bar.val(bar.val(0)); // <- ERROR: two mut borrows
}
3 posts - 2 participants
🏷️ Rust_feed