Rust
HAN, 26 jan 2024
Folkert de Vries
Rust
Me
Program
- why is rust different?
- using rust to move the needle
- the rust embedded ecosystem
Why Rust?
why is rust different?
Memory Safety
no segfaults, ever!
Memory Safety
fn main() {
println!("value is {}", *helper());
}
fn helper() -> &u64 {
let a = 42;
&a
}
Memory Safety
error[E0106]: missing lifetime specifier
--> src/main.rs:5:16
|
5 | fn helper() -> &u64 {
| ^ expected named lifetime parameter
|
= help: this function's return type contains a borrowed value, but there is no value for it to be borrowed from
help: consider using the `'static` lifetime
|
5 | fn helper() -> &'static u64 {
| +++++++
For more information about this error, try `rustc --explain E0106`.
error: could not compile `playground` (bin "playground") due to previous error
Memory Safety
fn main() {
println!("value is {}", *helper());
}
fn helper() -> &'static u64 {
let a = 42;
&a
}
Memory Safety
error[E0515]: cannot return reference to local variable `a`
--> src/main.rs:7:5
|
7 | &a
| ^^ returns a reference to data owned by the current function
For more information about this error, try `rustc --explain E0515`.
Memory Safety
installation
cargo fmt
one consistent format for all rust code everywhere
cargo check
cargo clippy
cargo build / cargo run
just works
cargo test
cargo ...
it really just works
standard library
ecosystem
consistent docs
Moving the Needle
making the internet memory-safe
ntpd-rs
Rustls
the fastest TLS implementation
sudo
"superuser do"
rav1d
An AV1 decoder in Rust.
future work
compression, codecs, critical infra
Rust Embedded
bare-metal rust
rust embedded ecosystem
cortex_m
pac
hal
rust embedded ecosystem
#![no_main]
#![no_std]
use embedded_hal::digital::v2::InputPin;
use embedded_hal::digital::v2::OutputPin;
use nrf52832_hal as hal;
use nrf52832_hal::gpio::Level;
use rtt_target::{rprintln, rtt_init_print};
#[panic_handler] // panicking behavior
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {
cortex_m::asm::bkpt();
}
}
#[cortex_m_rt::entry]
fn main() -> ! {
rtt_init_print!();
let p = hal::pac::Peripherals::take().unwrap();
let port0 = hal::gpio::p0::Parts::new(p.P0);
let button = port0.p0_13.into_pullup_input();
let mut led = port0.p0_17.into_push_pull_output(Level::Low);
rprintln!("Blinky button demo starting");
loop {
if button.is_high().unwrap() {
led.set_high().unwrap();
} else {
led.set_low().unwrap();
}
}
}
rust embedded ecosystem
#![no_main]
#![no_std]
use embedded_hal::digital::v2::InputPin;
use embedded_hal::digital::v2::OutputPin;
use nrf52832_hal as hal;
use nrf52832_hal::gpio::Level;
use rtt_target::{rprintln, rtt_init_print};
rust embedded ecosystem
#[panic_handler] // panicking behavior
fn panic(_: &core::panic::PanicInfo) -> ! {
loop {
cortex_m::asm::bkpt();
}
}
rust embedded ecosystem
#[cortex_m_rt::entry]
fn main() -> ! {
rtt_init_print!();
let p = hal::pac::Peripherals::take().unwrap();
let port0 = hal::gpio::p0::Parts::new(p.P0);
let button = port0.p0_13.into_pullup_input();
let mut led = port0.p0_17.into_push_pull_output(Level::Low);
rprintln!("Blinky button demo starting");
loop {
if button.is_high().unwrap() {
led.set_high().unwrap();
} else {
led.set_low().unwrap();
}
}
}
rust embedded ecosystem
packages/traits make code sharing almost trivial
tooling is excellent and not tied to a particular company
Next Steps
rust 101
meetups & conference
Conclusion
rust is a modern language,
you should try it!
Thanks
Copy of Rust HAN
By folkert de vries
Copy of Rust HAN
- 29