Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

_ 表达式

Syntax
UnderscoreExpression_

下划线表达式,用符号 _ 表示,用于在解构赋值中表示占位符。

它们只能出现在赋值的左侧。

请注意,这与[通配符模式][wildcard pattern]不同。

_ 表达式的示例:

#![allow(unused)]
fn main() {
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;
}