[rust-dev] Another idea on requiring "mut" prefix at call site

Noam Yorav-Raphael noamraph at gmail.com
Wed Apr 30 00:33:11 PDT 2014


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:

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?
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.mozilla.org/pipermail/rust-dev/attachments/20140430/c6f1e638/attachment.html>

More information about the Rust-dev mailing list