Reputation: 153
I'm reading Rust's internals and on https://github.com/rust-lang/rust/blob/master/compiler/rustc_hir/src/hir.rs it has
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>),
it looks like it's an enum with things that are considered expressions. Expressions are things that can be evaluated to a value. So funcion calls, closures, MethodCall etc can be seen as expressions. But this enum has things like Loop
. Can a loop be evaluated to something? What about an Array? Struct?
Upvotes: 0
Views: 559
Reputation: 5348
Rust is an expression oriented language, so lots of things which are traditionally considered statements in other languages are actually expressions, such as most control flow. Loops for example are expressions, See the rust book for more details. However, at the moment of the various forms of loop, only loop
expressions can have a value other than !
or ()
, as all other loop expressions have an implicit break condition;
As for array and struct, these represent the corresponding literals. So array expression kind refers to an array literal: [1, 2, 3]
An array literals value is an array of those values specified. The same is true for structs.
Upvotes: 2