[][src]Module ructe::Template_syntax::a_Value_expressions

A value expression can be as simple as @name to get the value of a parameter, but more complicated expressions, including function calls, are also allowed.

Value expressions

A parameter can be used in an expression preceded by an @ sign.

<h1>@name</h1>

If a parameter is a struct or a trait object, its fields or methods can be used, and if it is a callable, it can be called.

<p>The user @user.name has email @user.get_email().</p>
<p>A function result is @function(with, three, arguments).</p>

Standard function and macros can also be used, e.g. for specific formatting needs:

<p>The value is @format!("{:.1}", float_value).</p>

If more complex expressions are needed, they can be put in parenthesis.

<p>The sum @a+3 is @(a+3).</p>

If a is 2, this exapands to:

<p>The sum 2+3 is 5.</p>

Anything is allowed in parenthesis, as long as parenthesis, brackets and string quotes are balanced. Note that this also applies to the parenthesis of a function call or the brackets of an index, so complex things like the following are allowed:

<p>Index: @myvec[t.map(|s| s.length()).unwrap_or(0)].</p>
<p>Argument: @call(a + 3, |t| t.something()).</p>

An expression ends when parenthesis and brackets are matched and it is followed by something not allowed in an expression. This includes whitespace and e.g. the < and @ characters. If an expression starts with an open parenthesis, the expression ends when that parentheis is closed. That is usefull if an expression is to be emmediatley followed by something that would be allowed in an expression.

<p>@arg</p>
<p>@arg.</p>
<p>@arg.@arg</p>
<p>@arg.len()</p>
<p>@(arg).len()</p>
<p>@((2_i8 - 3).abs())</p>@* Note extra parens needed here *@

With arg = "name", the above renders as:

<p>name</p>
<p>name.</p>
<p>name.name</p>
<p>4</p>
<p>name.len()</p>
<p>1</p>