From 78cd7345599c5ae65f21ac7d3127e7b0d1f6e707 Mon Sep 17 00:00:00 2001 From: "Juan A. Pedreira" Date: Sun, 3 Mar 2024 15:20:34 +0100 Subject: [PATCH] underscore-expr: add more examples --- src/expressions/underscore-expr.md | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/expressions/underscore-expr.md b/src/expressions/underscore-expr.md index 3d170408b..d68c6a998 100644 --- a/src/expressions/underscore-expr.md +++ b/src/expressions/underscore-expr.md @@ -10,10 +10,25 @@ side of an assignment. Note that this is distinct from the [wildcard pattern](../patterns.md#wildcard-pattern). -An example of an `_` expression: +Examples of `_` expressions: ```rust let p = (1, 2); let mut a = 0; (_, a) = p; + +struct Position { + x: u32, + y: u32, +} + +Position { x: a, y: _ } = Position{ x: 2, y: 3 }; + +// unused result, assignment to `_` used to declare intent and remove a warning +_ = 2 + 2; +// triggers unused_must_use warning +// 2 + 2; + +// equivalent technique using a wildcard pattern in a let-binding +let _ = 2 + 2; ```