What?

https://github.com/brson/being-rust

  • Rust hype
  • Memory management
  • Rust history
  • How I work with Rust
  • How to learn Rust
  • Beyond tech
  • Guru notes

Also

  • Q&A
  • Stories
  • Live mistake-making!

About me (brson)

  • Rust since 2010
  • Mozilla / Rust core team
  • PingCAP since 2018
  • talent-plan/rust
brson.github.io

About you

  • Self-motivated
  • Some programming experience
  • Have read The Rust Book

Hype!

Elevator Pitch

Rust is a programming language that does exactly what you tell it to. It is one of the fastest languages in the world, but it does not crash like comparable languages. It is suitable for the most performance- and safety-critical systems, as well as for daily software development, and it runs everywhere.

Key features

  • Memory safety
  • Deterministic memory management
  • No runtime (no GC)
  • Ownership and borrowing
  • Platform support

Rust use cases

  • CLI tools
  • Operating systems
  • Runtimes
  • Multi-platform
  • Databases
  • Networking
  • Microcontrollers
  • Firmware
  • Rendering
  • Web backends
  • Web frontends (WASM)
  • Aerospace
  • Blockchains
  • Finance

Rust users

User: Mozilla

  • Product: Firefox
  • What: Web browser
  • Why: Security
  • Why: Performance

https://github.com/servo/servo

User: PingCAP

  • Product: TiDB (TiKV)
  • What: Database
  • Why: Reliability
  • Why: Performance

https://github.com/tikv/tikv

User: Cloudflare

  • Product: TiDB (TiKV)
  • What: Edge compute sandboxing
  • Why: WASM

https://github.com/cloudflare/quiche

Rust quotes and press

TODO

https://github.com/brson/my-rust-lists/blob/master/rust-quotes-and-press.md

Questions?

Memory management

The CPU and RAM

+---------------+           +---------------+
|               |           |               |
|   I'm the     |           |    I'm the    |
|     CPU       +-----------+      RAM      |
|               |           |               |
|               |     ^     |               |
+---------------+     |     +---------------+
                      |
               (this is slow)
            

The CPU and RAM

5b080  <alloc::box_free>:
5b080: 48 89 f0     mov    %rsi, %rax
5b083: 48 8b 76 08  mov    0x8(%rsi), %rsi
5b087: 48 85 f6     test   %rsi, %rsi
5b08a: 74 0a        je     5b096 <alloc::box_free+0x16>
            

The callstack

Source code

fn main() {
    let a = 1;
    foo(&a);
}
                

Stack frames

|=============| 0x00
| fn main     |
| a = 1       |
|=============|
                

Source code

fn main() {
    let a = 1;
    foo(&a);
}

fn foo(ptr_a: &i32) {
    let b = 2;
    bar(ptr_a, &b);
}
                

Stack frames

|=============| 0x00
| fn main     |
| a = 1       |
|=============| 0x10
| fn foo      |
| <main regs> |
| ptr_a = &a  |
| b = 2       |
|=============|
                

Source code

fn foo(ptr_a: &i32) {
    let b = 2;
    bar(ptr_a, &b);
}

fn bar(ptr_a: &i32,
       ptr_b: &i32) {
    ...
}
                

Stack frames

 ...
|=============| 0x10
| fn foo      |
| <main regs> |
| ptr_a = &a  |
| b = 2       |
|=============| 0x20
| fn bar      |
| <foo regs>  |
| ptr_a = &a  |
| ptr_b = &b  |
|=============|
                

Source code

fn main() {
    let a = 1;
    foo(&a);
}

fn foo(ptr_a: &i32) {
    let b = 2;
    bar(ptr_a, &b);
}
                

Stack frames

|=============| 0x00
| fn main     |
| a = 1       |
|=============| 0x10
| fn foo      |
| <main regs> |
| ptr_a = &a  |
| b = 2       |
|=============|
                

Source code

fn main() {
    let a = 1;
    foo(&a);
}
                

Stack frames

|=============| 0x00
| fn main     |
| a = 1       |
|=============|
                

The heap

  • Not CPU registers
  • Not the stack
  • All the other RAM
  • As allocated with malloc

Questions?

Managing the heap

  • manually with alloc / free (like C)
  • garbage collection (like Go)

Heap management examples

https://github.com/brson/being-rust/blob/master/examples/

Problems of malloc/free

  • Use after free
  • Double free
  • Crashes == security bugs

Problems of garbage collection

  • Bookeeping overhead
  • CPU overhead
  • Latency
  • Runtime binary
  • Mutable aliasing

Ownership in Rust

Questions?

Rust history

Questions?

How I work with Rust

Questions?

How to learn Rust

Rust mindset

  • Be curious
  • Write real projects
  • Read real code
  • Try again
  • Ask for help

Rust mindset

  • Be curious
  • Write real projects
  • Read real code
  • Try again
  • Ask for help

Rust resources

See README.md

Beyond tech

Relationships

Insecurity

Contributing

Inspiration

Questions?

Understand mutable aliasing

This is the thing Rust solves.

See README.md for links

Don't jump to lifetimes

Just clone stuff.

  • `memcpy` is fast.
  • `malloc` is fast.

Don't use `#[bench]`

Use criterion.

https://github.com/bheisler/criterion.rs

`impl Drop` is a breaking change

struct Bar;
struct Foo(Bar);
impl Drop for Foo { fn drop(&mut self) { } }

fn main() {
    // XXX can't destructure
    let Foo(i) = Foo(Bar);
}
            

Know the `cargo` command line

The defaults are not always what you want. Save time by being precise with your `cargo` invocations.

cargo check --all
cargo test --all --no-run
cargo check --all --no-run --profile=dev
cargo test -p subproject
            

The "turbofish"

a::<B>();

Alternate debug formatting

println!("{:?}", qux);
println!("{:#?}", qux);
            

Foo { bar: 0, baz: 1 }
Foo {
    bar: 0,
    baz: 1,
}
            

Building without the networking

cargo generate-lockfile
cargo fetch
cargo build --locked --offline
cargo test --locked --offline
            

Rust CI tools

cargo clippy
cargo fmt
cargo outdated
cargo audit
            

Questions?