02-路径表达式

路径表达式 — The Rust Reference

译文 · 基于 The Rust Reference

原文链接: https://doc.rust-lang.org/reference/expressions/path-expr.html

r[expr.path]

路径表达式

r[expr.path.syntax]

PathExpression ->
      PathInExpression
    | QualifiedPathInExpression

r[expr.path.intro] 在表达式上下文中使用的路径表示局部变量或项。

r[expr.path.place] 解析为局部变量或静态变量的路径表达式是位置表达式;其他路径是值表达式。

r[expr.path.safety] 使用 static mut 变量需要 unsafe 块。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
## mod globals {
##     pub static STATIC_VAR: i32 = 5;
##     pub static mut STATIC_MUT_VAR: i32 = 7;
## }
## let local_var = 3;
local_var;
globals::STATIC_VAR;
unsafe { globals::STATIC_MUT_VAR };
let some_constructor = Some::<i32>;
let push_integer = Vec::<i32>::push;
let slice_reverse = <[i32]>::reverse;

r[expr.path.const] 关联常量的求值方式与 const 块相同。

最后修改 August 21, 2026: 更新 (76fc81a2e)