Can I destructure a tuple without binding the result to a new variable in a let/match/for statement?

Yes.

The Rust team has published a new version of Rust 1.59.0 in Feb. 24, 2022, you can now use tuple, slice, and struct patterns as the left-hand side of an assignment.

Announcing Rust 1.59.0

Destructuring assignments

You can now use tuple, slice, and struct patterns as the left-hand side of an assignment.

let (a, b, c, d, e);

(a, b) = (1, 2); [c, .., d, _] = [1, 2, 3, 4, 5]; Struct { e, .. } =
Struct { e: 5, f: 3 };

assert_eq!([1, 2, 1, 4, 5], [a, b, c, d, e]);

This makes assignment more consistent with let bindings, which have
long supported the same thing. Note that destructuring assignments
with operators such as += are not allowed.

Before 1.59.0, you can only destructure it in Nightly version with #![feature(destructuring_assignment)].

Now you can do this trick in stable version and remove the feature line.

See more details from rust-lang/rust/issues/71126 and rust-lang/rust/pull/90521.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)