|
|
|
|
|
by tomjakubowski
4030 days ago
|
|
> I've been learning Rust, which requires notation at the call site that you're passing a mutable reference, and thus far, I quite prefer it; I had been concerned that it might be "too much typing", but it's proven to be rare and unobtrusive. I'm so glad Rust made this design choice, because it means that a programmer can see very quickly where borrows are happening and understand where and where not new borrows could be inserted. Take this example: struct Widget {
name: String
}
fn jabberwock_the_widget(w: &mut Widget) {
w.name.push_str(" (modified)");
}
fn main() {
let jimbo = Widget {
name: "Jimbo".to_string()
};
let name = &jimbo.name;
jabberwock_the_widget(&mut jimbo);
}
If that call were just `jabberwock_the_widget(jimbo)`, and I didn't have `jabberwock_the_widget`'s signature at hand, I would have to run the code through a compiler to see that there is an illegal mutable borrow on jimbo. With mutable borrows written so explicitly, it's clear at the call site that it can't happen with the existing borrow on the widget's name. |
|
Similarly if you call a method, there's no indication at all how `self` is being passed.