Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Perhaps things are different in rust, but in C assignment to something which ought to not change (either by the = vs == typo or some higher level cognitive error) is not too uncommon source of bug.

Being explicit about what data should be immutable or not as a way of expressing intent helps eliminate these bugs. In rust, the mutability is analyzed strongly by the compiler and is important for the ownership rules, so I'd expected the use of the mut flag to be more successful than const is in C / C++.



Specifically, note that mistaken use of `=` vs. `==` is impossible in Rust regardless of this proposal, since:

1. assignment is an expression that returns nil

2. `if` expressions require their conditions to evaluate to boolean

3. no types can be implicitly coerced to boolean

An example:

  fn main() {
      let mut x = 2;
      if x = 4 {}  // error: mismatched types: expected `bool` but found `()`
  }




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: