MANNING
Timothy Samuel McNamara
Systems programming concepts
and techniques
Raw Pointer
The cousins mut T and*
*const T are the free radicals
of the pointer world. Lightning
fast, but wildly unsafe.
Powers
• Speed
• Can interact with
the outside world
Weaknesses
• Unsafe
Box<T>
Store anything in a box. Accepts
almost any type for long-term
storage. The workhorse of a
new, safe programming era.
Powers
• Store a value in
central storage
in a location
called “the heap”
Weaknesses
• Size increase
Rc<T>
The reference counted pointer, Rc<T>
is Rust's competent, yet miserly
bookkeeper. It knows who has
borrowed what and when.
Powers
• Shared access
to values
Weaknesses
• Size increase
• Runtime cost
• Not threadsafe
Cell<T>
An expert in metamorphosis,
Cell<T> confers the ability to
mutate immutable values.
Powers
• Interior mutability
Weaknesses
• Size increase
• Performance
RefCell<T>
Performs mutation on immutable
references with RefCel<T>.
Its mind-bending powers
come with some costs.
Weaknesses
• Size increase
• Runtime cost
• Lack of compile-
time guarantees
Cow<T>
Why write something down when
you only need to read it? Perhaps
you only want to make modifications.
This is the role of Cow (copy on write).
Powers
• Avoids writes
when only read
access is used
Weaknesses
• Possible size
increase
Arc<T>
Your program’s main storage system.
Vec<T> keeps your data orderly
as values are created and destroyed.
Powers
• Grows dynamically
as required
Weaknesses
• Can over
allocate size
RawVec<T>
The bedrock of Vec<T> and
other dynamically sized types.
Understands how to provide a
home for your data as needed.
Powers
• Grows dynamically
as required
• Works with the
memory allocator
to find space
Weaknesses
• Not directly
applicable from
your code
Unique<T>
Sole owner of a value,
a unique pointer is guaranteed
to possess full control.
Powers
• Base for types
such as Strings,
requiring exclusive
possession of values.
Weaknesses
• Not appropriate
for application
code directly
Arc<T>
Arc<T> is Rust’s ambassador.
It can share values across threads,
guaranteeing that these will
not interfere with each other.
Powers
• Shared access
to values
• Threadsafe
Weaknesses
• Size increase
• Runtime cost
String
Acting as a guide on how to
deal with the uncertainties of
user input, String shows us how
to build safe abstractions.
Powers
• Grows dynamically
as required
• Guarantees correct
encoding at runtime
Weaknesses
• Can over
allocate size
Shared<T>
Sharing ownership is hard.
Shared<T> makes life
a little bit easier.
Powers
• Shared ownership
• Can align memory
to T’s width, even
when empty
Weaknesses
• Not appropriate
for application
code directly
Powers
• Interior mutability
• Can be nested
within Rc and Arc,
which only accept
immutable refs
Rust in Action
Rust in Action
SYSTEMS PROGRAMMING
CONCEPTS AND TECHNIQUES
TIM MCNAMARA
MANNING
SHELTER ISLAND