Rust is a compiled, statically and strongly typed language. Main features:
- memory safety
- low-level performance
- zero-cost abstractions
- non-nullable references
Install Rust via rustup
, which installs all Rust tools and cargo
— Rust's
official package manager and orchestration tool.
For a playground during learning, run cargo new playground
, edit main.rs
and
run by cargo run
in the project root directory.
- primitives like
i32
,f64
,bool
- arrays are fixed-size collections of values of the same type,
[i32; 100]
- tuples are collections of values of different types,
(i32, i32, &str)
- references allow borrowing data,
&value
- slices offer referencing data sequences with a variable size,
[T]
,str
- structs hold related data and define related methods
,
vector.normalize()
- enums define variants of types,
Result<T, E>
and itsOk(T)
,Err(E)
- generics allow parameters of different types
Vec<T>
- traits define behavior that types have like
ToString
- DSTs are types with size not known at compile-time,
dyn Trait
,[T]
,str
- lifetimes ensure data validity,
'static
,'a
- closures are anonymous functions that capture their context,
|x| x * 2
- attributes are metadata for crates, modules or items,
#[attr]
,#![crate_attr]
- macros for metaprogramming, declarative and
procedural,
println!("a macro")
or#[derive(Debug)]
Comments are specified after //
. No special multiline syntax.
snake_case
for crates, but prefer single-wordsnake_case
for functions, variables, macros and lifetimesPascalCase
for enums, structs, generics and traitsSCREAMING_SNAKE_CASE
for constants and statics