Appendix B · Operators and symbols.
Read punctuation as type, borrowing, comparison, and control-flow information.
The idea behind the syntax.
Rust's symbols carry distinct jobs. = assigns a value, == compares values, & borrows, * can dereference, :: separates path segments, and ! marks a macro call or logical negation depending on context. The same visible symbol can participate in different grammatical forms. Its meaning becomes clear from the expression and type around it, not from a memorised one-line translation.
Arithmetic operators act on suitable numeric types, and comparison operators produce bool. Operators can be implemented by traits for user-defined types, so + on a custom type may have a domain meaning. That flexibility does not permit arbitrary implicit conversions: operand types still have to satisfy the relevant trait implementation.
What Rust is checking.
The ? operator is special to control flow: in a compatible function, it unwraps a successful Option or Result and returns early on absence or error. The .. and ..= range forms describe exclusive and inclusive endpoints. A semicolon discards an expression's value, while a trailing expression without one can be a block's result. These small symbols often explain an otherwise surprising inferred type.
Symbols also mark ownership boundaries. &T is a shared reference type; &mut T is an exclusive mutable reference type. A function argument of T may take ownership, while a parameter of &T does not. The examples below connect notation to behavior rather than presenting a bare chart. When a symbol looks unfamiliar, identify whether it is part of a type, a value expression, a pattern, or a path before interpreting it.
See the rule in context.
Each example isolates one part of the model. Follow the bindings, types, ownership transfers, and possible outcomes line by line.
Ownership & moves
A value has an owner responsible for dropping it when its lifetime ends. Assigning a String to another binding transfers ownership rather than duplicating its heap allocation. The original binding is then unavailable. Passing an owned value into a function can transfer ownership in the same way; returning it can transfer it back. This makes cleanup predictable without requiring a garbage collector. Distinguish the owned String from an &str reference, which temporarily views text owned elsewhere.
fn label(mut name: String) -> String {
name.push_str(" / ready");
name
}
fn main() {
let first = String::from("engine");
let second = first;
let result = label(second);
println!("{result}");
}engine / ready
- The first-to-second assignment moves the String.
- label takes ownership and may mutate its local binding.
- The returned String becomes owned by result and is dropped at the end of main.
The boundary: A moved binding cannot be used even if you know the heap bytes still exist. The compiler follows ownership, not intuition about addresses.
Shared references & borrowing
A shared reference &T borrows a value without transferring ownership. Several shared references can coexist because safe Rust prevents conflicting ordinary mutation during those active borrows. A reference must not outlive its referent. Rust often ends a borrow after its last use, rather than waiting for the closing brace; this is why a later mutation can be allowed once earlier reads are finished. Prefer parameters such as &str or &[T] when a function only needs a view rather than ownership of a particular container.
fn characters(text: &str) -> usize {
text.chars().count()
}
fn main() {
let name = String::from("Navan");
let size = characters(&name);
println!("{name}: {size} characters");
}Navan: 5 characters
- &name borrows instead of moving the String.
- The String reference can coerce to &str.
- The caller still owns name after the function returns.
The boundary: String::len counts UTF-8 bytes, while chars().count counts Unicode scalar values; neither automatically counts user-visible graphemes.
if, else & boolean logic
An if condition must be a bool; numbers do not convert to truthiness automatically. An if/else can produce a value, provided both branches have compatible types. Logical && and || short-circuit, so the second operand is evaluated only when needed. This lets a guard protect an operation such as division. State conditions in terms of your domain rather than clever punctuation, and test the boundary where the decision changes.
fn main() {
let age = 19;
let status = if age >= 18 { "adult" } else { "minor" };
let denominator = 0;
let safe = denominator != 0 && 10 / denominator > 1;
println!("{status}, safe={safe}");
}adult, safe=false
- The two status branches both return string slices.
- The false first operand prevents division by zero.
- Test age 17 and 18 to exercise the boundary.
The boundary: Use parentheses to clarify mixed && and || expressions when the business rule could be misread.
Keep this model.
Punctuation is meaningful only in context. Locate the surrounding type and expression first, then read the symbol's role.