循环可以是表达式吗?
Can a loop be an expression?
我正在阅读 Rust 的内部结构,https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir/src/hir.rs 它有
pub enum ExprKind<'hir> {
/// A `box x` expression.
Box(&'hir Expr<'hir>),
/// Allow anonymous constants from an inline `const` block
ConstBlock(AnonConst),
/// An array (e.g., `[a, b, c, d]`).
Array(&'hir [Expr<'hir>]),
/// A function call.
///
/// The first field resolves to the function itself (usually an `ExprKind::Path`),
/// and the second field is the list of arguments.
/// This also represents calling the constructor of
/// tuple-like ADTs such as tuple structs and enum variants.
Call(&'hir Expr<'hir>, &'hir [Expr<'hir>]),
/// A method call (e.g., `x.foo::<'static, Bar, Baz>(a, b, c, d)`).
///
/// The `PathSegment`/`Span` represent the method name and its generic arguments
/// (within the angle brackets).
/// The first element of the vector of `Expr`s is the expression that evaluates
/// to the object on which the method is being called on (the receiver),
/// and the remaining elements are the rest of the arguments.
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
/// The final `Span` represents the span of the function and arguments
/// (e.g. `foo::<Bar, Baz>(a, b, c, d)` in `x.foo::<Bar, Baz>(a, b, c, d)`
///
/// To resolve the called method to a `DefId`, call [`type_dependent_def_id`] with
/// the `hir_id` of the `MethodCall` node itself.
///
/// [`type_dependent_def_id`]: ../ty/struct.TypeckResults.html#method.type_dependent_def_id
MethodCall(&'hir PathSegment<'hir>, Span, &'hir [Expr<'hir>], Span),
/// A tuple (e.g., `(a, b, c, d)`).
Tup(&'hir [Expr<'hir>]),
/// A binary operation (e.g., `a + b`, `a * b`).
Binary(BinOp, &'hir Expr<'hir>, &'hir Expr<'hir>),
/// A unary operation (e.g., `!x`, `*x`).
Unary(UnOp, &'hir Expr<'hir>),
/// A literal (e.g., `1`, `"foo"`).
Lit(Lit),
/// A cast (e.g., `foo as f64`).
Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
它看起来像是一个枚举,其中包含被视为表达式的内容。表达式是可以被评估为一个值的东西。所以函数调用、闭包、MethodCall 等都可以看作是表达式。但是这个枚举有 Loop
之类的东西。可以对循环进行评估吗?数组呢?结构?
Rust 是一种面向表达式的语言,所以很多在其他语言中传统上被认为是语句的东西实际上是表达式,比如大多数控制流。例如,循环是表达式 See the rust book for more details。但是,在各种形式的循环中,只有loop
表达式可以有!
或()
以外的值,因为所有其他循环表达式都有隐式中断条件;
对于array和struct,这些代表对应的字面值。所以数组表达式 kind 指的是数组文字: [1, 2, 3]
数组文字值是指定的那些值的数组。结构体也是如此。
我正在阅读 Rust 的内部结构,https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir/src/hir.rs 它有
pub enum ExprKind<'hir> {
/// A `box x` expression.
Box(&'hir Expr<'hir>),
/// Allow anonymous constants from an inline `const` block
ConstBlock(AnonConst),
/// An array (e.g., `[a, b, c, d]`).
Array(&'hir [Expr<'hir>]),
/// A function call.
///
/// The first field resolves to the function itself (usually an `ExprKind::Path`),
/// and the second field is the list of arguments.
/// This also represents calling the constructor of
/// tuple-like ADTs such as tuple structs and enum variants.
Call(&'hir Expr<'hir>, &'hir [Expr<'hir>]),
/// A method call (e.g., `x.foo::<'static, Bar, Baz>(a, b, c, d)`).
///
/// The `PathSegment`/`Span` represent the method name and its generic arguments
/// (within the angle brackets).
/// The first element of the vector of `Expr`s is the expression that evaluates
/// to the object on which the method is being called on (the receiver),
/// and the remaining elements are the rest of the arguments.
/// Thus, `x.foo::<Bar, Baz>(a, b, c, d)` is represented as
/// `ExprKind::MethodCall(PathSegment { foo, [Bar, Baz] }, [x, a, b, c, d])`.
/// The final `Span` represents the span of the function and arguments
/// (e.g. `foo::<Bar, Baz>(a, b, c, d)` in `x.foo::<Bar, Baz>(a, b, c, d)`
///
/// To resolve the called method to a `DefId`, call [`type_dependent_def_id`] with
/// the `hir_id` of the `MethodCall` node itself.
///
/// [`type_dependent_def_id`]: ../ty/struct.TypeckResults.html#method.type_dependent_def_id
MethodCall(&'hir PathSegment<'hir>, Span, &'hir [Expr<'hir>], Span),
/// A tuple (e.g., `(a, b, c, d)`).
Tup(&'hir [Expr<'hir>]),
/// A binary operation (e.g., `a + b`, `a * b`).
Binary(BinOp, &'hir Expr<'hir>, &'hir Expr<'hir>),
/// A unary operation (e.g., `!x`, `*x`).
Unary(UnOp, &'hir Expr<'hir>),
/// A literal (e.g., `1`, `"foo"`).
Lit(Lit),
/// A cast (e.g., `foo as f64`).
Cast(&'hir Expr<'hir>, &'hir Ty<'hir>),
它看起来像是一个枚举,其中包含被视为表达式的内容。表达式是可以被评估为一个值的东西。所以函数调用、闭包、MethodCall 等都可以看作是表达式。但是这个枚举有 Loop
之类的东西。可以对循环进行评估吗?数组呢?结构?
Rust 是一种面向表达式的语言,所以很多在其他语言中传统上被认为是语句的东西实际上是表达式,比如大多数控制流。例如,循环是表达式 See the rust book for more details。但是,在各种形式的循环中,只有loop
表达式可以有!
或()
以外的值,因为所有其他循环表达式都有隐式中断条件;
对于array和struct,这些代表对应的字面值。所以数组表达式 kind 指的是数组文字: [1, 2, 3]
数组文字值是指定的那些值的数组。结构体也是如此。