embedded-trainings-2020/advanced/firmware/src/bin/vec.rs

35 lines
1 KiB
Rust
Raw Normal View History

#![deny(unused_must_use)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
2021-06-04 11:41:31 +00:00
use heapless::Vec;
2021-04-14 10:13:32 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use firmware as _;
#[entry]
fn main() -> ! {
dk::init().unwrap();
// a stack-allocated `Vec` with capacity for 6 bytes
2021-06-04 11:41:31 +00:00
let mut buffer = Vec::<u8, 6>::new();
// content type ^^ ^ capacity
// `heapless::Vec` exposes the same API surface as `std::Vec` but some of its methods return a
// `Result` to indicate whether the operation failed due to the `heapless::Vec` being full
2022-01-07 16:24:21 +00:00
defmt::println!("start: {:?}", buffer);
buffer.push(0).expect("buffer full");
2022-01-07 16:24:21 +00:00
defmt::println!("after `push`: {:?}", buffer);
buffer.extend_from_slice(&[1, 2, 3]).expect("buffer full");
2022-01-07 16:24:21 +00:00
defmt::println!("after `extend`: {:?}", &buffer);
// TODO try this operation
// buffer.extend_from_slice(&[4, 5, 6, 7]).expect("buffer full");
// TODO try changing the capacity of the `heapless::Vec`
dk::exit()
}