Just because there is precedent, doesn’t mean that we need to add more. I would like to minimize this as much as possible. Also, this is harder to spot than slice indexing, because slice indexing is quite a bit less noisy compared to arbitrary patterns. With patterns, you could accidentally borrow the same RefCell multiple times which could lead to confusing panics.
let a = RefCell::new(10);
// ... many lines later ...
let b = (&a, &a); // something that borrows a twice, doesn't have to be a tuple
// ... many lines later ...
match b {
(box ref c, box ref mut d) => {
// this code is garuenteed to panic, because of overlapping borrows
// What should the error message be? The stack trace?
}
}
This is equivalent to
use std::cell::RefCell;
fn main() {
let a = RefCell::new(0);
let b = (&a, &a);
let c = a.borrow();
let d = a.borrow_mut();
}
But in this case the panic message can more easily point to what went wrong, because the line numbers are more useful.
Now, lets imagine a more complex pattern that is using this, with more references to the RefCell and it is easy to see how this can turn into a debugging nightmare.