Cpp Cheatsheet
Rust Fundamentals
Overview
- Writing Safe Rust gets you the following benefits instantly:
- running UBSAN, ASAN, THREADSAN and RAII analysis at compile time, without the performance penalty at runtime
- all bindings are
const
ed by default unless they specifically opt out withlet mut
- all code you depend on is also analyzed under the same constraints by the compiler
Installation
- LLVM is the default codegen backend but the experimental gcc has not yet stabilized
- CodeLLDB is a reliable debugger setup
Basic Types
- Raw pointers do exist but are rarely used. Their typed variants with added semantics and safety are preferred, like
&
. - Rust Strings do not have the Small String Optimization that C++ does
- Try a drop in replacement like smallstr instead
Basic Types 2
- Rust's
println!
semantics for non-numerics follow those ofsprintf
, but with{}
:%-10s
to format a left aligned string padded to minimum 10 spaces becomes{:<10}
%04
to pad a number with zeros up to a width of 4 becomes{:04}
, etc.
- Rust does not have user defined literals so you need a macro to make
let duration = 5_milliseconds;
work in Rust.
Basic Types 3
- A Rust
enum
is most similar to anstd::variant
=
and+=
like operators return the value that was set, whereas in Rust they do not.
Control Flow
- This loop
const auto v = {1,2,3,4};
for (const auto &: list) {
//...
}
is equivalent to
for value in &list {
//...
}