Enum Expr
enum Expr
A Rust expression.
This type is available only if Syn is built with the "derive" or "full"
feature, but most of the variants are not available unless "full" is enabled.
Syntax tree enums
This type is a syntax tree enum. In Syn this and other syntax tree enums are designed to be traversed using the following rebinding idiom.
# use Expr;
#
#
We begin with a variable expr of type Expr that has no fields
(because it is an enum), and by matching on it and rebinding a variable
with the same name expr we effectively imbue our variable with all of
the data fields provided by the variant that it turned out to be. So for
example above if we ended up in the MethodCall case then we get to use
expr.receiver, expr.args etc; if we ended up in the If case we get
to use expr.cond, expr.then_branch, expr.else_branch.
This approach avoids repeating the variant names twice on every line.
# use ;
#
#
In general, the name to which a syntax tree enum variant is bound should be a suitable name for the complete syntax tree enum type.
# use ;
#
#
A sign that you may not be choosing the right variable names is if you
see names getting repeated in your code, like accessing
receiver.receiver or pat.pat or cond.cond.
Variants
-
Array(ExprArray) A slice literal expression:
[a, b, c, d].-
Assign(ExprAssign) An assignment expression:
a = compute().-
Async(ExprAsync) An async block:
async { ... }.-
Await(ExprAwait) An await expression:
fut.await.-
Binary(ExprBinary) A binary operation:
a + b,a += b.-
Block(ExprBlock) A blocked scope:
{ ... }.-
Break(ExprBreak) A
break, with an optional label to break and an optional expression.-
Call(ExprCall) A function call expression:
invoke(a, b).-
Cast(ExprCast) A cast expression:
foo as f64.-
Closure(ExprClosure) A closure expression:
|a, b| a + b.-
Const(ExprConst) A const block:
const { ... }.-
Continue(ExprContinue) A
continue, with an optional label.-
Field(ExprField) Access of a named struct field (
obj.k) or unnamed tuple struct field (obj.0).-
ForLoop(ExprForLoop) A for loop:
for pat in expr { ... }.-
Group(ExprGroup) An expression contained within invisible delimiters.
This variant is important for faithfully representing the precedence of expressions and is related to
None-delimited spans in aTokenStream.-
If(ExprIf) An
ifexpression with an optionalelseblock:if expr { ... } else { ... }.The
elsebranch expression may only be anIforBlockexpression, not any of the other types of expression.-
Index(ExprIndex) A square bracketed indexing expression:
vector[2].-
Infer(ExprInfer) The inferred value of a const generic argument, denoted
_.-
Let(ExprLet) A
letguard:let Some(x) = opt.-
Lit(ExprLit) A literal in place of an expression:
1,"foo".-
Loop(ExprLoop) Conditionless loop:
loop { ... }.-
Macro(ExprMacro) A macro invocation expression:
format!("{}", q).-
Match(ExprMatch) A
matchexpression:match n { Some(n) => {}, None => {} }.-
MethodCall(ExprMethodCall) A method call expression:
x.foo::<T>(a, b).-
Paren(ExprParen) A parenthesized expression:
(a + b).-
Path(ExprPath) A path like
std::mem::replacepossibly containing generic parameters and a qualified self-type.A plain identifier like
xis a path of length 1.-
Range(ExprRange) A range expression:
1..2,1..,..2,1..=2,..=2.-
RawAddr(ExprRawAddr) Address-of operation:
&raw const placeor&raw mut place.-
Reference(ExprReference) A referencing operation:
&aor&mut a.-
Repeat(ExprRepeat) An array literal constructed from one repeated element:
[0u8; N].-
Return(ExprReturn) A
return, with an optional value to be returned.-
Struct(ExprStruct) A struct literal expression:
Point { x: 1, y: 1 }.The
restprovides the value of the remaining fields as inS { a: 1, b: 1, ..rest }.-
Try(ExprTry) A try-expression:
expr?.-
TryBlock(ExprTryBlock) A try block:
try { ... }.-
Tuple(ExprTuple) A tuple expression:
(a, b, c, d).-
Unary(ExprUnary) A unary operation:
!x,*x.-
Unsafe(ExprUnsafe) An unsafe block:
unsafe { ... }.-
Verbatim(proc_macro2::TokenStream) Tokens in expression position not interpreted by Syn.
-
While(ExprWhile) A while loop:
while expr { ... }.-
Yield(ExprYield) A yield expression:
yield expr.
Implementations
impl Expr
fn parse_without_eager_brace(input: ParseStream<'_>) -> Result<Expr>An alternative to the primary
Expr::parseparser (from theParsetrait) for ambiguous syntactic positions in which a trailing brace should not be taken as part of the expression.Rust grammar has an ambiguity where braces sometimes turn a path expression into a struct initialization and sometimes do not. In the following code, the expression
S {}is one expression. Presumably there is an empty structstruct S {}defined somewhere which it is instantiating.# ; # let _ = *S ; // parsed by rustc as: `*(S {})`We would want to parse the above using
Expr::parseafter the=token.But in the following,
S {}is not a struct init expression.# const S: &bool = &true; if *S // parsed by rustc as: // // if (*S) { // /* empty block */ // } // { // /* another empty block */ // }For that reason we would want to parse if-conditions using
Expr::parse_without_eager_braceafter theiftoken. Same for similar syntactic positions such as the condition expr after awhiletoken or the expr at the top of amatch.The Rust grammar's choices around which way this ambiguity is resolved at various syntactic positions is fairly arbitrary. Really either parse behavior could work in most positions, and language designers just decide each case based on which is more likely to be what the programmer had in mind most of the time.
# ; # // parsed by rustc as: // // if (return (S {})) { // } // // but could equally well have been this other arbitrary choice: // // if (return S) { // } // {}Note the grammar ambiguity on trailing braces is distinct from precedence and is not captured by assigning a precedence level to the braced struct init expr in relation to other operators. This can be illustrated by
return 0..S {}vsmatch 0..S {}. The former parses asreturn (0..(S {}))implying tighter precedence for struct init than.., while the latter parses asmatch (0..S) {}implying tighter precedence for..than struct init, a contradiction.fn parse_with_earlier_boundary_rule(input: ParseStream<'_>) -> Result<Expr>An alternative to the primary
Expr::parseparser (from theParsetrait) for syntactic positions in which expression boundaries are placed more eagerly than done by the typical expression grammar. This includes expressions at the head of a statement or in the right-hand side of amatcharm.Compare the following cases:
# let result = ; # let guard = false; # let cond = true; # let f = true; # let g = f; # let _ = match result ;# let cond = true; # let f = ; # let g = f; # let _ = ;# let cond = true; # let f = ; # let g = f; # let _ = ;The same sequence of tokens
if cond { f } else { g } ()appears in expression position 3 times. The first two syntactic positions use eager placement of expression boundaries, and parse asExpr::If, with the adjacent()becomingPat::TupleorExpr::Tuple. In contrast, the third case uses standard expression boundaries and parses asExpr::Call.As with
parse_without_eager_brace, this ambiguity in the Rust grammar is independent of precedence.fn peek(input: ParseStream<'_>) -> boolReturns whether the next token in the parse stream is one that might possibly form the beginning of an expr.
This classification is a load-bearing part of the grammar of some Rust expressions, notably
returnandbreak. For examplereturn < …will never parse<as a binary operator regardless of what comes after, because<is a legal starting token for an expression and so it's required to be continued as a return value, such asreturn <Struct as Trait>::CONST. Meanwhilereturn > …treats the>as a binary operator because it cannot be a starting token for any Rust expression.
impl Clone for Expr
fn clone(self: &Self) -> Self
impl Debug for Expr
fn fmt(self: &Self, formatter: &mut Formatter<'_>) -> Result
impl Eq for Expr
impl Freeze for Expr
impl From for Expr
fn from(e: ExprAwait) -> Expr
impl From for Expr
fn from(e: ExprCast) -> Expr
impl From for Expr
fn from(e: ExprForLoop) -> Expr
impl From for Expr
fn from(e: ExprLet) -> Expr
impl From for Expr
fn from(e: ExprMethodCall) -> Expr
impl From for Expr
fn from(e: ExprReference) -> Expr
impl From for Expr
fn from(e: ExprTryBlock) -> Expr
impl From for Expr
fn from(e: ExprYield) -> Expr
impl From for Expr
fn from(e: ExprArray) -> Expr
impl From for Expr
fn from(e: ExprBlock) -> Expr
impl From for Expr
fn from(e: ExprConst) -> Expr
impl From for Expr
fn from(e: ExprIf) -> Expr
impl From for Expr
fn from(e: ExprLoop) -> Expr
impl From for Expr
fn from(e: ExprPath) -> Expr
impl From for Expr
fn from(e: ExprReturn) -> Expr
impl From for Expr
fn from(e: ExprUnary) -> Expr
impl From for Expr
fn from(e: ExprAsync) -> Expr
impl From for Expr
fn from(e: ExprCall) -> Expr
impl From for Expr
fn from(e: ExprField) -> Expr
impl From for Expr
fn from(e: ExprInfer) -> Expr
impl From for Expr
fn from(e: ExprMatch) -> Expr
impl From for Expr
fn from(e: ExprRawAddr) -> Expr
impl From for Expr
fn from(e: ExprTry) -> Expr
impl From for Expr
fn from(e: ExprWhile) -> Expr
impl From for Expr
fn from(e: ExprBinary) -> Expr
impl From for Expr
fn from(e: ExprClosure) -> Expr
impl From for Expr
fn from(e: ExprGroup) -> Expr
impl From for Expr
fn from(e: ExprLit) -> Expr
impl From for Expr
fn from(e: ExprParen) -> Expr
impl From for Expr
fn from(e: ExprRepeat) -> Expr
impl From for Expr
fn from(e: ExprTuple) -> Expr
impl From for Expr
fn from(e: ExprAssign) -> Expr
impl From for Expr
fn from(e: ExprBreak) -> Expr
impl From for Expr
fn from(e: ExprContinue) -> Expr
impl From for Expr
fn from(e: ExprIndex) -> Expr
impl From for Expr
fn from(e: ExprMacro) -> Expr
impl From for Expr
fn from(e: ExprRange) -> Expr
impl From for Expr
fn from(e: ExprStruct) -> Expr
impl From for Expr
fn from(e: ExprUnsafe) -> Expr
impl Hash for Expr
fn hash<H>(self: &Self, state: &mut H) where H: Hasher
impl Parse for Expr
fn parse(input: ParseStream<'_>) -> Result<Self>
impl PartialEq for Expr
fn eq(self: &Self, other: &Self) -> bool
impl RefUnwindSafe for Expr
impl Send for Expr
impl Sync for Expr
impl ToTokens for Expr
fn to_tokens(self: &Self, tokens: &mut TokenStream)
impl Unpin for Expr
impl UnsafeUnpin for Expr
impl UnwindSafe for Expr
impl<T> Any for Expr
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Expr
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Expr
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Expr
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Expr
fn from(t: T) -> TReturns the argument unchanged.
impl<T> Spanned for Expr
fn span(self: &Self) -> Span
impl<T> ToOwned for Expr
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for Expr
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Expr
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Expr
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>