Random Rust Snippets

date: 2021-04-12
author: Peterino

Static Strlist

const BROWSERS: &'static [&'static str] = &["firefox", "chrome"];

Singletons in rust

struct Peripherals {
    serial: Option<SerialPort>,
}

impl Peripherals {
    fn take_serial(&mut self) -> SerialPort {
        let p = replace(&mut self.serial, None);
        p.unwrap()
    }
}

static mut PERIPHERALS: Peripherals = Peripherals {
    serial: Some(SerialPort),
};

Rust Macros

macro_rules! info {
    ($($arg:tt)*) => ({
        ::std::print!("[sear ]: ");
        ::std::println!($($arg)*);
    })
}