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

Tabel B-1 berisi operator-operator dalam Cairo, contoh bagaimana operator akan muncul dalam konteks, penjelasan singkat, dan apakah operator tersebut dapat di-overload. Jika suatu operator dapat di-overload, trait yang relevan untuk digunakan untuk meng-overload operator tersebut tercantum.

OperatorContohExplanationOverloadable?
!!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 / exprPembagian aritmetikaDiv
/=var /= exprPembagian dan penugasan aritmetikaDivEq
:pat: type, ident: typeConstraints
:ident: exprPenginisialisasi bidang struktur
;expr;Pernyataan dan terminator item
<expr < exprPerbandingan kurang dariPartialOrd
<=expr <= exprPerbandingan kurang dari atau sama denganPartialOrd
=var = exprAssignment
==expr == exprPerbandingan kesetaraanPartialEq
=>pat => exprPart of match arm syntax
>expr > exprPerbandingan lebih besar dariPartialOrd
>=expr >= exprPerbandingan lebih besar dari atau sama denganPartialOrd
^expr ^ exprBitwise exclusive ORBitXor
|expr | exprBitwise ORBitOr
||expr || exprShort-circuiting logical OR
?expr?Error propagation

Table B-1: Operators

Non Operator Symbols

Daftar berikut berisi semua simbol yang tidak digunakan sebagai operator; artinya, mereka tidak memiliki perilaku yang sama seperti panggilan fungsi atau metode.

Tabel B-2 menunjukkan simbol-simbol yang muncul sendiri dan valid dalam berbagai lokasi.

SymbolExplanation
..._u8, ..._usize, ..._bool, etc.Literal numerik dengan tipe tertentu
"..."String literal
'...'Short string, 31 ASCII characters maximum
_“Ignored” pattern binding

Table B-2: Stand-Alone Syntax

Tabel B-3 menunjukkan simbol-simbol yang digunakan dalam konteks jalur hierarki modul untuk mengakses suatu item.

SymbolExplanation
ident::identNamespace path
super::pathJalur relatif terhadap induk modul saat ini
trait::method(...)Membedakan panggilan metode dengan menamai trait yang mendefinisikannya

Table B-3: Path-Related Syntax

Tabel B-4 menunjukkan simbol-simbol yang muncul dalam konteks penggunaan parameter tipe generik.

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<...> ...Mendefinisikan fungsi generik
struct ident<...> ...Mendefinisikan struktur generik
enum ident<...> ...Define generic enumeration
impl<...> ...Mendefinisikan implementasi generik

Table B-4: Generics

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
()Tuple kosong (alias unit), baik literal maupun tipe
(expr)Parenthesized expression
`expr + exprSingle-element tuple expression
(type,)Single-element tuple type
(expr, ...)Tuple expression
(type, ...)Tuple type
expr(expr, ...)Ekspresi panggilan fungsi; juga digunakan untuk menginisialisasi struktur tuple dan varian tuple enumerasi

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