I think I see the confusion (as I suffered from the same point of confusion). So let me restate your answer and please correct me of I am wrong. 1. "mut int" and "& mut int" are different types and the former doesn't automatically convert to the latter. 2. The way to get the latter from the former is to say "&mut i" since "&i" is defined as taking a non-mut borrow even if i is mut. (This was the point of confusion I believe.) 3. No explicit conversion is needed within foo() since the type of i is already "&mut int".
Ashish On Dec 28, 2013 1:33 PM, "Kevin Ballard" <[email protected]> wrote: > We have to say `&mut i` in main() because `&i` is non-mutable. We’re > explicitly taking a mutable borrow. > > But once it’s in foo(), it’s *already* mutable. The type `&mut int` > carries its mutability with it. Having to say `mut` again makes no sense > and is nothing but pure noise. > > -Kevin > > On Dec 27, 2013, at 4:59 PM, Vadim <[email protected]> wrote: > > For the same reason we currently have to say `&mut i` in main() - to > explicitly acknowledge that the callee may mutate i. By the same logic, > this should be done everywhere. > > > On Wed, Dec 25, 2013 at 3:11 PM, Kevin Ballard <[email protected]> wrote: > >> On Dec 25, 2013, at 5:17 PM, Vadim <[email protected]> wrote: >> >> 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; >> } >> >> >> What is the point of adding `mut` here? bar() does not need `mut` because >> calling bar(i) does not auto-borrow i. It’s *already* a `&mut int`. >> >> -Kevin >> > > > > _______________________________________________ > Rust-dev mailing list > [email protected] > https://mail.mozilla.org/listinfo/rust-dev > >
_______________________________________________ Rust-dev mailing list [email protected] https://mail.mozilla.org/listinfo/rust-dev
