Glossary
Each term has a short definition and, where one exists, the nearest Java (or C#) equivalent. “Nearest” is not “same”: read the definition.
- Borrow, reference
Access to a value without owning it. A shared reference
&Tlets you read; a mutable reference&mut Tlets you change. At any time a value has either many shared references or one mutable reference. Java: every object variable is a reference, but Java has no rules about who can read or change.- Borrow checker
The part of the compiler that checks that every reference is valid and that the borrow rules hold. Most “fighting the compiler” stories are about it. Java: none.
- Cargo
Rust’s build tool and package manager:
cargo build,cargo test,cargo add. Java: Maven or Gradle.- Clippy
The official linter, run with
cargo clippy. Its lints are grouped: default,pedantic,nursery, andrestriction. Java: SpotBugs, Error Prone, or IntelliJ inspections.- Clone
A trait for explicit copies:
value.clone(). It can be cheap (anArc) or expensive (a largeVec). Java:clone()or a copy constructor, but in Rust you see every copy in the code.- Closure
An anonymous function that can capture variables:
|o| o.total_cents > min. Java: a lambda, but a Rust closure can also change captured variables.- Copy
A marker trait for small types that copy bit by bit on assignment, instead of moving: integers,
bool,char, and structs that derive it. Java: primitive types.- Crate
The unit of compilation: a library or a binary. Published crates live on crates.io. Java: a JAR; C#: an assembly.
- Derive
#[derive(Debug, Clone, PartialEq)]: an attribute that generates trait implementations. Java: what Lombok or your IDE generates (toString,equals,hashCode).- Enum, variant
A type that is one of several variants, and each variant can carry its own data. Java: a sealed interface with one record per case (more than a Java
enum).- Expression
Code that produces a value. In Rust,
if,match, and blocks are expressions. The last expression in a block, without;, is the block’s value. Java: the ternary operator andswitchexpressions.- Lifetime
The span of code where a reference is valid. An annotation such as
'anames a lifetime so that the compiler can relate references; it does not change how long a value lives. Java: none.- Macro
Code that generates code at compile time. A call has a
!(println!,vec!); attributes such as#[derive]are macros too. Java: annotation processors; there is no direct equivalent toname!(...).- Match, pattern
matchcompares a value against patterns and runs the first arm that fits. It must cover every case. Patterns can destructure, bind names, and test ranges. Java: aswitchexpression with patterns.- Module
A namespace inside a crate:
mod orders;. Privacy works at the module level, not the type level. Java: a package, roughly.- Move
Transfer of ownership. Assignment and passing a non-
Copyvalue to a function move it, and the old name cannot be used after that. Java: none; Java copies the reference and both names stay valid.- Option
Option<T>isSome(value)orNone. Rust has nonull, so a missing value is always anOption. Java:Optional<T>, but used everywhere, including fields and parameters.- Ownership
The rule that every value has exactly one owner. When the owner goes out of scope, the value is dropped (freed). Java: none; the garbage collector frees objects. C#: like
using/IDisposablefor every value.- Panic
An unrecoverable error that unwinds the current thread (or aborts the process).
unwrap(),expect(),panic!, and out-of-range indexing panic. Java: anErroror an uncaughtRuntimeException; not for normal control flow.- Question mark operator (
?) After an expression of type
ResultorOption: return the error (orNone) to the caller now, or continue with the value. Java: like letting a checked exception propagate, but visible on each line.- Result
Result<T, E>isOk(value)orErr(error). Errors are values in the return type, not exceptions. Java: a checked exception in thethrowsclause.- Shadowing
Declaring a new variable with the same name as an earlier one:
let total = total * 2;. The old one is hidden. Normal in Rust. Java: not allowed for local variables.- Slice
A borrowed view of part or all of a sequence:
&[T]for a list,&strfor a string. Java:List.subListor aCharSequence, but without copying and checked by the compiler.- Smell
On this site: a pattern that compiles but makes code worse. Each smell has a permanent ID (S01 to S38). See the smell catalogue.
- String and
&str Stringis an owned, growable UTF-8 string.&stris a borrowed string slice, for example a literal or part of aString. Java:StringBuilder/StringforString; a read-onlyCharSequenceview for&str.- Struct,
impl A
structholds named fields. Methods are defined separately in animplblock. There is no inheritance. Java: a class without inheritance; a record when the fields are public and immutable.- Trait
A set of methods that types can implement, possibly with default implementations. Java: an interface with default methods. C#: an interface, plus the ability to implement it for types you did not write (like extension methods).
- Trait bound
A requirement on a generic type:
T: Display, or in awhereclause. Java:<T extends Comparable<T>>.- Trait object (
dyn Trait) A value of some type that implements a trait, known only at run time, called through a vtable. Usually behind a pointer:
Box<dyn Trait>,&dyn Trait. Java: every interface-typed variable.- Turbofish (
::<>) A generic type argument given at the call site:
text.parse::<u32>(). Java:Collections.<String>emptyList().- Unwrap
unwrap()takes the value out of anOptionorResultand panics if there is none.expect("msg")does the same with a message. Java:Optional.get()ororElseThrow().- Vec
Vec<T>, the growable list type. Java:ArrayList<T>.