Just because I still think it might be a good idea -- if it turns out to be
a good idea, it will be possible to add such a "mut" annotation to rust 1.1
and issue a warning if it is not being used, right?

Noam
On Apr 30, 2014 3:31 PM, "Noam Yorav-Raphael" <[email protected]> wrote:

> Actually I realize that my suggestion doesn't solve any real problem.
> The example at reddit was quite strange, and as Patrick wrote,
> avoiding autoborrow of ~ to & would solve it.
>
> The real place where such an annotation would help is where, like in
> Artella's example, a function gets a &mut reference and passes it to
> another function. Then it's not clear whether the other function
> expects a non-mutable reference or is it going to mutate the variable.
>
> Then a solution might be to require writing "mut" before the name of a
> &mut reference that can't be replaced with a non-mut reference. That
> is, in this example:
>
> fn mut_it(x: &mut int) {
>     *x += 1;
> }
>
> fn print_it(x: &int) {
>     println!("{}", x);
> }
>
> fn f(x: &mut int) {
>     mut_it(x);
>     print_it(x);
> }
>
> fn main() {
>     let mut x = 1i;
>     f(&mut x);
> }
>
> You'll have to replace "mut_it(x)" with "mut_it(mut x)"
>
> On Wed, Apr 30, 2014 at 10:33 AM, Noam Yorav-Raphael <[email protected]>
> wrote:
> > Hi,
> >
> > I had a bug caused by a function mutating its arguments, and it had
> occurred
> > to me that it may be a good idea if rust would require a "mut" prefix in
> > that case. I asked on reddit, and was referred to this thread:
> > https://mail.mozilla.org/pipermail/rust-dev/2014-January/007670.html
> >
> > In the above message, Patrick shows a few examples which show that it's
> hard
> > to come up with rules on which arguments should be prefixed by "mut" that
> > will be sound and complete. I have an idea which may be. The idea is to
> not
> > look at function arguments but at uses of a variable. Here's a rule:
> >
> > Whenever a variable which was declared with "let mut" is being used in a
> way
> > that would have been illegal have it not been declared with "let mut", it
> > should be prefixed by "mut", unless it's obvious from the context that it
> > has to be mutable.
> >
> > I think it's quite simple and says exactly what should be the rules in
> > Patrick's examples. What's not well-defined is the "obvious from the
> > context" part. Certainly when a variable is on the left hand side of an
> > assignment there would be no need for "mut" annotation, as well as when
> it's
> > being prefixed by "&mut". I don't know if there are other cases.
> >
> > (If you're interested in the bug: I had to use a function solve(A, b)
> which
> > gets a matrix A and a vector b and returns a vector x such that Ax=b. It
> > does Gauss elimination, and for efficiency it modified A and b instead of
> > allocating new arrays. I used it like x = solve(A, b) and then used A
> again.
> > It was in Fortran, so the arguments A and b were annotated as being "in
> > out", but of course it didn't stop my perfectly looking function from
> having
> > a hidden bug.)
> >
> > What do you think?
> > Noam
>
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev

Reply via email to