On Dec 28, 2013, at 1:53 PM, Ashish Myles <[email protected]> wrote:

> 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.
> 
Effectively correct. `mut int` isn’t actually a type, `int` is a type and the 
`mut` here means that the slot is mutable. `&mut int` is a type. You can in 
fact have `mut i: &mut int` to have a mutable slot containing a `&mut int`. 
This would allow you to replace the pointer stored in `i` with a different 
pointer. If the slot is not mutable, the pointer is fixed but because it’s a 
`&mut int` the data that’s being pointed to can be modified.
> 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.)
> 
Correct.

There is in fact one way to automatically borrow `mut i` to `&mut i`, and 
that’s when calling a method on `i` that takes `&mut self`. But I believe 
that’s the only way to automatically perform this borrowing.
> 3. No explicit conversion is needed within foo() since the type of i is 
> already "&mut int”.
> 
Correct.

-Kevin
> 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

Reply via email to