I agree that unexpected mutation is undesirable, but:
- requiring 'mut' is orthogonal to requiring '&' sigil, IMHO,
- as currently implemented, Rust does not always require mut when callee
mutates the argument, for example:

fn main() {
    let mut i: int = 0;
    foo(&mut i);
    println!("{}", i);
}
fn foo(i: &mut int) {
    bar(i); // no mut!
}
fn bar(i: &mut int) {
    *i = *i + 1;
}

Note that invocation of bar() inside foo() does not forewarn reader by
requiring 'mut'.  Wouldn't you rather see this?:

fn main() {
    let mut i: int = 0;
    foo(mut i);
    println!("{}", i);
}
fn foo(i: &mut int) {
    bar(mut i);
}
fn bar(i: &mut int) {
    i = i + 1;
}



On Tue, Dec 24, 2013 at 6:10 PM, comex <[email protected]> wrote:

> On Tue, Dec 24, 2013 at 8:17 PM, Vadim <[email protected]> wrote:
> > I'm again pretty nervous about this, because this is one of the
> > much-maligned features of C++ references.
> >
> >
> > Why is it maligned, though?
>
> I malign it mainly because it allows (and encourages) confusing code
> that looks like it's calling a function with a variable as an argument
> but actually mutates the variable.  Arguably, only auto-borrowing
> immutably would sidestep this issue.
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to