Types

Contents

Types#

Rust is statically typed with a small set of primitives, two composite forms (struct and enum), references, slices, strings, and a generic type system with traits. The compiler infers locally; the operator annotates at function boundaries.

For the literal forms, see Syntax. For traits, see OOP.

Pages#

Primitives

i* / u* / f*, bool, char, (), !.

Primitives
Strings

String and &str; bytes vs chars.

Strings
Tuples

Anonymous fixed-size, mixed-type records.

Tuples
Arrays and slices

[T; N], &[T], Vec<T>.

Arrays and slices
References

&T, &mut T, Option<&T> for nullable.

References
Structs

Named, tuple, and unit forms; struct update.

Structs
Enums

Discriminated unions; variants carry data.

Enums
Option and Result

Null replacement; exception replacement; ? operator.

Option and Result
Generics

<T> type parameters, trait bounds, where clauses.

Generics
Lifetimes

'a region annotations; 'static.

Lifetimes
Trait objects

&dyn Trait, Box<dyn Trait>; vtable dispatch.

Trait objects
Type aliases

type Name = ... vs newtype structs.

Type aliases
Casting

as; From / Into; TryFrom / TryInto.

Casting

References#