Right, that's how it works now.   But I was speculating on how it could
work with auto-borrow.  Specifically, I was addressing comex's concern that
C++-like reference auto-borrowing would make it non-obvious when the callee
might mutate the value.

You could have said "Well, I've already declared my variable as mutable,
i.e. `let mut i = 0`.  Since is already mutable, why do I have to say "mut"
again when borrowing?  The compiler could have easily inferred that."   I
believe the answer is "To help readers of this code realize that the called
function is [most likely] going to mutate the variable".   I believe the
same logic should apply to mut references.

I know that when I write my code, *on the caller side*, I am much more
concerned about which calls are going to mutate my variables, then about
whether they are passed by-value or by-reference.  After all, the callee
has already chosen that for me.
And, judging by the number of C++ projects that ban non-const references
but nave no problem with const ones, I think that a significant proportion
of developers feel similarly.

Vadim



On Sat, Dec 28, 2013 at 10:03 AM, Kevin Ballard <ke...@sb.org> 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 <vadi...@gmail.com> 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 <ke...@sb.org> wrote:
>
>> On Dec 25, 2013, at 5:17 PM, Vadim <vadi...@gmail.com> 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
Rust-dev@mozilla.org
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to