252 lines
17 KiB
HTML
252 lines
17 KiB
HTML
<!DOCTYPE html>
|
||
<html lang="en">
|
||
<head>
|
||
<meta charset="UTF-8">
|
||
<title>B - Operators and Symbols</title>
|
||
</head>
|
||
<body>
|
||
<h2 id="appendix-b-operators-and-symbols"><a class="header" href="#appendix-b-operators-and-symbols">Appendix B: Operators and Symbols</a></h2>
|
||
<p>This appendix contains a glossary of Rust’s syntax, including operators and
|
||
other symbols that appear by themselves or in the context of paths, generics,
|
||
trait bounds, macros, attributes, comments, tuples, and brackets.</p>
|
||
<h3 id="operators"><a class="header" href="#operators">Operators</a></h3>
|
||
<p>Table B-1 contains the operators in Rust, an example of how the operator would
|
||
appear in context, a short explanation, and whether that operator is
|
||
overloadable. If an operator is overloadable, the relevant trait to use to
|
||
overload that operator is listed.</p>
|
||
<p><span class="caption">Table B-1: Operators</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Operator</th><th>Example</th><th>Explanation</th><th>Overloadable?</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>!</code></td><td><code>ident!(...)</code>, <code>ident!{...}</code>, <code>ident![...]</code></td><td>Macro expansion</td><td></td></tr>
|
||
<tr><td><code>!</code></td><td><code>!expr</code></td><td>Bitwise or logical complement</td><td><code>Not</code></td></tr>
|
||
<tr><td><code>!=</code></td><td><code>expr != expr</code></td><td>Nonequality comparison</td><td><code>PartialEq</code></td></tr>
|
||
<tr><td><code>%</code></td><td><code>expr % expr</code></td><td>Arithmetic remainder</td><td><code>Rem</code></td></tr>
|
||
<tr><td><code>%=</code></td><td><code>var %= expr</code></td><td>Arithmetic remainder and assignment</td><td><code>RemAssign</code></td></tr>
|
||
<tr><td><code>&</code></td><td><code>&expr</code>, <code>&mut expr</code></td><td>Borrow</td><td></td></tr>
|
||
<tr><td><code>&</code></td><td><code>&type</code>, <code>&mut type</code>, <code>&'a type</code>, <code>&'a mut type</code></td><td>Borrowed pointer type</td><td></td></tr>
|
||
<tr><td><code>&</code></td><td><code>expr & expr</code></td><td>Bitwise AND</td><td><code>BitAnd</code></td></tr>
|
||
<tr><td><code>&=</code></td><td><code>var &= expr</code></td><td>Bitwise AND and assignment</td><td><code>BitAndAssign</code></td></tr>
|
||
<tr><td><code>&&</code></td><td><code>expr && expr</code></td><td>Short-circuiting logical AND</td><td></td></tr>
|
||
<tr><td><code>*</code></td><td><code>expr * expr</code></td><td>Arithmetic multiplication</td><td><code>Mul</code></td></tr>
|
||
<tr><td><code>*=</code></td><td><code>var *= expr</code></td><td>Arithmetic multiplication and assignment</td><td><code>MulAssign</code></td></tr>
|
||
<tr><td><code>*</code></td><td><code>*expr</code></td><td>Dereference</td><td><code>Deref</code></td></tr>
|
||
<tr><td><code>*</code></td><td><code>*const type</code>, <code>*mut type</code></td><td>Raw pointer</td><td></td></tr>
|
||
<tr><td><code>+</code></td><td><code>trait + trait</code>, <code>'a + trait</code></td><td>Compound type constraint</td><td></td></tr>
|
||
<tr><td><code>+</code></td><td><code>expr + expr</code></td><td>Arithmetic addition</td><td><code>Add</code></td></tr>
|
||
<tr><td><code>+=</code></td><td><code>var += expr</code></td><td>Arithmetic addition and assignment</td><td><code>AddAssign</code></td></tr>
|
||
<tr><td><code>,</code></td><td><code>expr, expr</code></td><td>Argument and element separator</td><td></td></tr>
|
||
<tr><td><code>-</code></td><td><code>- expr</code></td><td>Arithmetic negation</td><td><code>Neg</code></td></tr>
|
||
<tr><td><code>-</code></td><td><code>expr - expr</code></td><td>Arithmetic subtraction</td><td><code>Sub</code></td></tr>
|
||
<tr><td><code>-=</code></td><td><code>var -= expr</code></td><td>Arithmetic subtraction and assignment</td><td><code>SubAssign</code></td></tr>
|
||
<tr><td><code>-></code></td><td><code>fn(...) -> type</code>, <code>|…| -> type</code></td><td>Function and closure return type</td><td></td></tr>
|
||
<tr><td><code>.</code></td><td><code>expr.ident</code></td><td>Field access</td><td></td></tr>
|
||
<tr><td><code>.</code></td><td><code>expr.ident(expr, ...)</code></td><td>Method call</td><td></td></tr>
|
||
<tr><td><code>.</code></td><td><code>expr.0</code>, <code>expr.1</code>, and so on</td><td>Tuple indexing</td><td></td></tr>
|
||
<tr><td><code>..</code></td><td><code>..</code>, <code>expr..</code>, <code>..expr</code>, <code>expr..expr</code></td><td>Right-exclusive range literal</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code>..=</code></td><td><code>..=expr</code>, <code>expr..=expr</code></td><td>Right-inclusive range literal</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code>..</code></td><td><code>..expr</code></td><td>Struct literal update syntax</td><td></td></tr>
|
||
<tr><td><code>..</code></td><td><code>variant(x, ..)</code>, <code>struct_type { x, .. }</code></td><td>“And the rest” pattern binding</td><td></td></tr>
|
||
<tr><td><code>...</code></td><td><code>expr...expr</code></td><td>(Deprecated, use <code>..=</code> instead) In a pattern: inclusive range pattern</td><td></td></tr>
|
||
<tr><td><code>/</code></td><td><code>expr / expr</code></td><td>Arithmetic division</td><td><code>Div</code></td></tr>
|
||
<tr><td><code>/=</code></td><td><code>var /= expr</code></td><td>Arithmetic division and assignment</td><td><code>DivAssign</code></td></tr>
|
||
<tr><td><code>:</code></td><td><code>pat: type</code>, <code>ident: type</code></td><td>Constraints</td><td></td></tr>
|
||
<tr><td><code>:</code></td><td><code>ident: expr</code></td><td>Struct field initializer</td><td></td></tr>
|
||
<tr><td><code>:</code></td><td><code>'a: loop {...}</code></td><td>Loop label</td><td></td></tr>
|
||
<tr><td><code>;</code></td><td><code>expr;</code></td><td>Statement and item terminator</td><td></td></tr>
|
||
<tr><td><code>;</code></td><td><code>[...; len]</code></td><td>Part of fixed-size array syntax</td><td></td></tr>
|
||
<tr><td><code><<</code></td><td><code>expr << expr</code></td><td>Left-shift</td><td><code>Shl</code></td></tr>
|
||
<tr><td><code><<=</code></td><td><code>var <<= expr</code></td><td>Left-shift and assignment</td><td><code>ShlAssign</code></td></tr>
|
||
<tr><td><code><</code></td><td><code>expr < expr</code></td><td>Less than comparison</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code><=</code></td><td><code>expr <= expr</code></td><td>Less than or equal to comparison</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code>=</code></td><td><code>var = expr</code>, <code>ident = type</code></td><td>Assignment/equivalence</td><td></td></tr>
|
||
<tr><td><code>==</code></td><td><code>expr == expr</code></td><td>Equality comparison</td><td><code>PartialEq</code></td></tr>
|
||
<tr><td><code>=></code></td><td><code>pat => expr</code></td><td>Part of match arm syntax</td><td></td></tr>
|
||
<tr><td><code>></code></td><td><code>expr > expr</code></td><td>Greater than comparison</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code>>=</code></td><td><code>expr >= expr</code></td><td>Greater than or equal to comparison</td><td><code>PartialOrd</code></td></tr>
|
||
<tr><td><code>>></code></td><td><code>expr >> expr</code></td><td>Right-shift</td><td><code>Shr</code></td></tr>
|
||
<tr><td><code>>>=</code></td><td><code>var >>= expr</code></td><td>Right-shift and assignment</td><td><code>ShrAssign</code></td></tr>
|
||
<tr><td><code>@</code></td><td><code>ident @ pat</code></td><td>Pattern binding</td><td></td></tr>
|
||
<tr><td><code>^</code></td><td><code>expr ^ expr</code></td><td>Bitwise exclusive OR</td><td><code>BitXor</code></td></tr>
|
||
<tr><td><code>^=</code></td><td><code>var ^= expr</code></td><td>Bitwise exclusive OR and assignment</td><td><code>BitXorAssign</code></td></tr>
|
||
<tr><td><code>|</code></td><td><code>pat | pat</code></td><td>Pattern alternatives</td><td></td></tr>
|
||
<tr><td><code>|</code></td><td><code>expr | expr</code></td><td>Bitwise OR</td><td><code>BitOr</code></td></tr>
|
||
<tr><td><code>|=</code></td><td><code>var |= expr</code></td><td>Bitwise OR and assignment</td><td><code>BitOrAssign</code></td></tr>
|
||
<tr><td><code>||</code></td><td><code>expr || expr</code></td><td>Short-circuiting logical OR</td><td></td></tr>
|
||
<tr><td><code>?</code></td><td><code>expr?</code></td><td>Error propagation</td><td></td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<h3 id="non-operator-symbols"><a class="header" href="#non-operator-symbols">Non-operator Symbols</a></h3>
|
||
<p>The following tables contain all symbols that don’t function as operators; that
|
||
is, they don’t behave like a function or method call.</p>
|
||
<p>Table B-2 shows symbols that appear on their own and are valid in a variety of
|
||
locations.</p>
|
||
<p><span class="caption">Table B-2: Stand-alone Syntax</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>'ident</code></td><td>Named lifetime or loop label</td></tr>
|
||
<tr><td>Digits immediately followed by <code>u8</code>, <code>i32</code>, <code>f64</code>, <code>usize</code>, and so on</td><td>Numeric literal of specific type</td></tr>
|
||
<tr><td><code>"..."</code></td><td>String literal</td></tr>
|
||
<tr><td><code>r"..."</code>, <code>r#"..."#</code>, <code>r##"..."##</code>, and so on</td><td>Raw string literal; escape characters not processed</td></tr>
|
||
<tr><td><code>b"..."</code></td><td>Byte string literal; constructs an array of bytes instead of a string</td></tr>
|
||
<tr><td><code>br"..."</code>, <code>br#"..."#</code>, <code>br##"..."##</code>, and so on</td><td>Raw byte string literal; combination of raw and byte string literal</td></tr>
|
||
<tr><td><code>'...'</code></td><td>Character literal</td></tr>
|
||
<tr><td><code>b'...'</code></td><td>ASCII byte literal</td></tr>
|
||
<tr><td><code>|…| expr</code></td><td>Closure</td></tr>
|
||
<tr><td><code>!</code></td><td>Always-empty bottom type for diverging functions</td></tr>
|
||
<tr><td><code>_</code></td><td>“Ignored” pattern binding; also used to make integer literals readable</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-3 shows symbols that appear in the context of a path through the module
|
||
hierarchy to an item.</p>
|
||
<p><span class="caption">Table B-3: Path-Related Syntax</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>ident::ident</code></td><td>Namespace path</td></tr>
|
||
<tr><td><code>::path</code></td><td>Path relative to the crate root (that is, an explicitly absolute path)</td></tr>
|
||
<tr><td><code>self::path</code></td><td>Path relative to the current module (that is, an explicitly relative path)</td></tr>
|
||
<tr><td><code>super::path</code></td><td>Path relative to the parent of the current module</td></tr>
|
||
<tr><td><code>type::ident</code>, <code><type as trait>::ident</code></td><td>Associated constants, functions, and types</td></tr>
|
||
<tr><td><code><type>::...</code></td><td>Associated item for a type that cannot be directly named (for example, <code><&T>::...</code>, <code><[T]>::...</code>, and so on)</td></tr>
|
||
<tr><td><code>trait::method(...)</code></td><td>Disambiguating a method call by naming the trait that defines it</td></tr>
|
||
<tr><td><code>type::method(...)</code></td><td>Disambiguating a method call by naming the type for which it’s defined</td></tr>
|
||
<tr><td><code><type as trait>::method(...)</code></td><td>Disambiguating a method call by naming the trait and type</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-4 shows symbols that appear in the context of using generic type
|
||
parameters.</p>
|
||
<p><span class="caption">Table B-4: Generics</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>path<...></code></td><td>Specifies parameters to a generic type in a type (for example, <code>Vec<u8></code>)</td></tr>
|
||
<tr><td><code>path::<...></code>, <code>method::<...></code></td><td>Specifies parameters to a generic type, function, or method in an expression; often referred to as <em>turbofish</em> (for example, <code>"42".parse::<i32>()</code>)</td></tr>
|
||
<tr><td><code>fn ident<...> ...</code></td><td>Define generic function</td></tr>
|
||
<tr><td><code>struct ident<...> ...</code></td><td>Define generic structure</td></tr>
|
||
<tr><td><code>enum ident<...> ...</code></td><td>Define generic enumeration</td></tr>
|
||
<tr><td><code>impl<...> ...</code></td><td>Define generic implementation</td></tr>
|
||
<tr><td><code>for<...> type</code></td><td>Higher ranked lifetime bounds</td></tr>
|
||
<tr><td><code>type<ident=type></code></td><td>A generic type where one or more associated types have specific assignments (for example, <code>Iterator<Item=T></code>)</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-5 shows symbols that appear in the context of constraining generic type
|
||
parameters with trait bounds.</p>
|
||
<p><span class="caption">Table B-5: Trait Bound Constraints</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>T: U</code></td><td>Generic parameter <code>T</code> constrained to types that implement <code>U</code></td></tr>
|
||
<tr><td><code>T: 'a</code></td><td>Generic type <code>T</code> must outlive lifetime <code>'a</code> (meaning the type cannot transitively contain any references with lifetimes shorter than <code>'a</code>)</td></tr>
|
||
<tr><td><code>T: 'static</code></td><td>Generic type <code>T</code> contains no borrowed references other than <code>'static</code> ones</td></tr>
|
||
<tr><td><code>'b: 'a</code></td><td>Generic lifetime <code>'b</code> must outlive lifetime <code>'a</code></td></tr>
|
||
<tr><td><code>T: ?Sized</code></td><td>Allow generic type parameter to be a dynamically sized type</td></tr>
|
||
<tr><td><code>'a + trait</code>, <code>trait + trait</code></td><td>Compound type constraint</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-6 shows symbols that appear in the context of calling or defining
|
||
macros and specifying attributes on an item.</p>
|
||
<p><span class="caption">Table B-6: Macros and Attributes</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>#[meta]</code></td><td>Outer attribute</td></tr>
|
||
<tr><td><code>#![meta]</code></td><td>Inner attribute</td></tr>
|
||
<tr><td><code>$ident</code></td><td>Macro substitution</td></tr>
|
||
<tr><td><code>$ident:kind</code></td><td>Macro metavariable</td></tr>
|
||
<tr><td><code>$(...)...</code></td><td>Macro repetition</td></tr>
|
||
<tr><td><code>ident!(...)</code>, <code>ident!{...}</code>, <code>ident![...]</code></td><td>Macro invocation</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-7 shows symbols that create comments.</p>
|
||
<p><span class="caption">Table B-7: Comments</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>//</code></td><td>Line comment</td></tr>
|
||
<tr><td><code>//!</code></td><td>Inner line doc comment</td></tr>
|
||
<tr><td><code>///</code></td><td>Outer line doc comment</td></tr>
|
||
<tr><td><code>/*...*/</code></td><td>Block comment</td></tr>
|
||
<tr><td><code>/*!...*/</code></td><td>Inner block doc comment</td></tr>
|
||
<tr><td><code>/**...*/</code></td><td>Outer block doc comment</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-8 shows the contexts in which parentheses are used.</p>
|
||
<p><span class="caption">Table B-8: Parentheses</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Symbol</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>()</code></td><td>Empty tuple (aka unit), both literal and type</td></tr>
|
||
<tr><td><code>(expr)</code></td><td>Parenthesized expression</td></tr>
|
||
<tr><td><code>(expr,)</code></td><td>Single-element tuple expression</td></tr>
|
||
<tr><td><code>(type,)</code></td><td>Single-element tuple type</td></tr>
|
||
<tr><td><code>(expr, ...)</code></td><td>Tuple expression</td></tr>
|
||
<tr><td><code>(type, ...)</code></td><td>Tuple type</td></tr>
|
||
<tr><td><code>expr(expr, ...)</code></td><td>Function call expression; also used to initialize tuple <code>struct</code>s and tuple <code>enum</code> variants</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-9 shows the contexts in which curly brackets are used.</p>
|
||
<p><span class="caption">Table B-9: Curly Brackets</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Context</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>{...}</code></td><td>Block expression</td></tr>
|
||
<tr><td><code>Type {...}</code></td><td>Struct literal</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
<p>Table B-10 shows the contexts in which square brackets are used.</p>
|
||
<p><span class="caption">Table B-10: Square Brackets</span></p>
|
||
<div class="table-wrapper">
|
||
<table>
|
||
<thead>
|
||
<tr><th>Context</th><th>Explanation</th></tr>
|
||
</thead>
|
||
<tbody>
|
||
<tr><td><code>[...]</code></td><td>Array literal</td></tr>
|
||
<tr><td><code>[expr; len]</code></td><td>Array literal containing <code>len</code> copies of <code>expr</code></td></tr>
|
||
<tr><td><code>[type; len]</code></td><td>Array type containing <code>len</code> instances of <code>type</code></td></tr>
|
||
<tr><td><code>expr[expr]</code></td><td>Collection indexing; overloadable (<code>Index</code>, <code>IndexMut</code>)</td></tr>
|
||
<tr><td><code>expr[..]</code>, <code>expr[a..]</code>, <code>expr[..b]</code>, <code>expr[a..b]</code></td><td>Collection indexing pretending to be collection slicing, using <code>Range</code>, <code>RangeFrom</code>, <code>RangeTo</code>, or <code>RangeFull</code> as the “index”</td></tr>
|
||
</tbody>
|
||
</table>
|
||
</div>
|
||
</body>
|
||
</html>
|