File: pattern-ref-bindings-reassignment.rs

package info (click to toggle)
rustc 1.90.0%2Bdfsg1-1~exp1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 925,948 kB
  • sloc: xml: 158,148; javascript: 19,781; sh: 19,174; python: 15,732; ansic: 13,096; cpp: 7,181; asm: 4,376; makefile: 697; lisp: 176; sql: 15
file content (24 lines) | stat: -rw-r--r-- 842 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//! Tests how we behave when the user attempts to mutate an immutable
//! binding that was introduced by either `ref` or `ref mut`
//! patterns.
//!
//! Such bindings cannot be made mutable via the mere addition of the
//! `mut` keyword, and thus we want to check that the compiler does not
//! suggest doing so.

fn main() {
    let (mut one_two, mut three_four) = ((1, 2), (3, 4));

    // Bind via pattern:
    // - `a` as immutable reference (`ref`)
    // - `b` as mutable reference (`ref mut`)
    let &mut (ref a, ref mut b) = &mut one_two;

    // Attempt to reassign immutable `ref`-bound variable
    a = &three_four.0;
    //~^ ERROR cannot assign twice to immutable variable `a`

    // Attempt to reassign mutable `ref mut`-bound variable
    b = &mut three_four.1;
    //~^ ERROR cannot assign twice to immutable variable `b`
}