Appendix B - Operators and Symbols

This appendix contains a glossary of Cairo's syntax, including operators and other symbols that appear by themselves or in the context of paths, generics, macros, attributes, comments, tuples, and brackets.

Operators

表B-1包含了开罗的运算符,运算符在上下文中出现的例子和简短的解释,以及该运算符是否可以重载。如果一个运算符是可重载的,则列出了用于重载该运算符的相关特性。

OperatorExampleExplanationOverloadable?
!!exprLogical complementNot
~~exprBitwise NOTBitNot
!=expr != exprNon-equality comparisonPartialEq
%expr % exprArithmetic remainderRem
%=var %= exprArithmetic remainder and assignmentRemEq
&expr & exprBitwise ANDBitAnd
&&expr && exprShort-circuiting logical AND
*expr * exprArithmetic multiplicationMul
*=var *= exprArithmetic multiplication and assignmentMulEq
@@varSnapshot
**varDesnap
+expr + exprArithmetic additionAdd
+=var += exprArithmetic addition and assignmentAddEq
,expr, exprArgument and element separator
--exprArithmetic negationNeg
-expr - exprArithmetic subtractionSub
-=var -= exprArithmetic subtraction and assignmentSubEq
->fn(...) -> type, |...| -> typeFunction and closure return type
.expr.identMember access
/expr / exprArithmetic divisionDiv
/=var /= exprArithmetic division and assignmentDivEq
:pat: type, ident: typeConstraints
:ident: exprStruct field initializer
;expr;Statement and item terminator
<expr < exprLess than comparisonPartialOrd
<=expr <= exprLess than or equal to comparisonPartialOrd
=var = exprAssignment
==expr == exprEquality comparisonPartialEq
=>pat => exprPart of match arm syntax
>expr > exprGreater than comparisonPartialOrd
>=expr >= exprGreater than or equal to comparisonPartialOrd
^expr ^ exprBitwise exclusive ORBitXor
|expr | exprBitwise ORBitOr
||expr || exprShort-circuiting logical OR
?expr?Error propagation

表B-1:运算符

Non Operator Symbols

下面的列表包含了所有不作为运算符使用的符号;也就是说,他们并不像函数调用或方法调用一样表现。

表B-2 展示了以其自身出现以及出现在合法其他各个地方的符号。

SymbolExplanation
..._u8, ..._usize, ..._bool, etc.Numeric literal of specific type
"..."String literal
'...'Short string, 31 ASCII characters maximum
_“Ignored” pattern binding

表B-2:独立语法

表B-3 展示了出现在从模块结构到项的路径上下文中的符号。

SymbolExplanation
ident::identNamespace path
super::pathPath relative to the parent of the current module
trait::method(...)Disambiguating a method call by naming the trait that defines it

表B-3:路径相关语法

表B-4 展示了出现在泛型类型参数上下文中的符号。

SymbolExplanation
path<...>Specifies parameters to generic type in a type (e.g., Array<u8>)
path::<...>, method::<...>Specifies parameters to a generic type, function, or method in an expression; often referred to as turbofish
fn ident<...> ...Define generic function
struct ident<...> ...Define generic structure
enum ident<...> ...Define generic enumeration
impl<...> ...Define generic implementation

表 B-4:泛型

Table B-5 shows symbols that appear in the context of specifying attributes on an item.

SymbolExplanation
#[derive(...)]Automatically implements a trait for a type
#[inline]Hint to the compiler to allow inlining of annotated function
#[inline(always)]Hint to the compiler to systematically inline annotated function
#[inline(never)]Hint to the compiler to never inline annotated function
#[must_use]Hint to the compiler that the return value of a function or a specific returned type must be used
#[generate_trait]Automatically generates a trait for an impl
#[available_gas(...)]Set the maximum amount of gas available to execute a function
#[panic_with('...', wrapper_name)]Creates a wrapper for the annotated function which will panic if the function returns None or Err, with the given data as the panic error
#[test]Describe a function as a test function
#[cfg(...)]Configuration attribute, especially used to configure a tests module with #[cfg(test)]
#[should_panic]Specifies that a test function should necessarily panic
#[starknet::contract]Defines a Starknet smart contract
#[starknet::interface]Defines a Starknet interface
#[starknet::component]Defines a Starknet component
#[starknet::embeddable]Defines an isolated embeddable implementation that can be injected in any smart contract
#[embeddable_as(...)]Defines an embeddable implementation inside a component
#[storage]Defines the storage of a smart contract
#[event]Defines an event in a smart contract
#[constructor]Defines the constructor in a smart contract
#[abi(embed_v0)]Defines an implementation of a trait, exposing the functions of the impl as entrypoints of a contract
#[abi(per_item)]Allows individual definition of the entrypoint type of functions inside an impl
#[external(v0)]Defines an external function when #[abi(per_item)] is used
#[flat]Defines a enum variant of the Event enum that is not nested, ignoring the variant name in the serialization process, very useful for composability when using Starknet components
#[key]Defines an indexed Event enum field, allowing for more efficient queries and filtering of events

Table B-5: Attributes

Table B-6 shows symbols that appear in the context of calling or defining macros.

SymbolExplanation
print!Inline printing
println!Print on a new line
consteval_int!Declare a constant that is the result of a computation of integers
array!Instantiate and fill arrays
panic!Calls panic function and allows to provide a message error longer than 31 characters
assert!Evaluates a Boolean and panics if false
assert_eq!Evaluates an equality, and panics if not equal
assert_ne!Evaluates an equality, and panics if equal
assert_lt!Evaluates a comparison, and panics if greater or equal
assert_le!Evaluates a comparison, and panics if greater
assert_gt!Evaluates a comparison, and panics if lower or equal
assert_ge!Evaluates a comparison, and panics if lower
format!Format a string and returns a ByteArray with the contents
write!Write formatted strings in a formatter
writeln!Write formatted strings in a formatter on a new line
get_dep_component!Returns the requested component state from a snapshot of the state inside a component
get_dep_component_mut!Returns the requested component state from a reference of the state inside a component
component!Macro used in Starknet contracts to embed a component inside a contract

Table B-6: Macros

Table B-7 shows symbols that create comments.

SymbolExplanation
//Line comment

Table B-7: Comments

Table B-8 shows symbols that appear in the context of using tuples.

SymbolExplanation
()Empty tuple (aka unit), both literal and type
(expr)Parenthesized expression
(expr,)Single-element tuple expression
(type,)Single-element tuple type
(expr, ...)Tuple expression
(type, ...)Tuple type
expr(expr, ...)Function call expression; also used to initialize tuple structs and tuple enum variants

Table B-8: Tuples

Table B-9 shows the contexts in which curly braces are used.

ContextExplanation
{...}Block expression
Type {...}struct literal

Table B-9: Curly Braces