embedded-trainings-2020/beginner/apps/src/bin/radio-puzzle-2.rs

40 lines
1.2 KiB
Rust
Raw Normal View History

2020-06-22 13:23:59 +00:00
#![deny(unused_must_use)]
#![no_main]
#![no_std]
use cortex_m_rt::entry;
// NOTE you can use `FnvIndexMap` instead of `LinearMap`; the former may have better
// lookup performance when the dictionary contains a large number of items but performance is
// not important for this exercise
2021-06-04 11:41:31 +00:00
use heapless::LinearMap;
2021-04-08 12:16:26 +00:00
// this imports `beginner/apps/lib.rs` to retrieve our global logger + panicking-behavior
use apps as _;
2020-06-22 13:23:59 +00:00
#[entry]
fn main() -> ! {
dk::init().unwrap();
// a dictionary with capacity for 2 elements
2021-06-04 11:41:31 +00:00
let mut dict = LinearMap::<_, _, 2>::new();
// content types ^^ ^^ ^ capacity
// (inferred by rust)
2020-06-22 13:23:59 +00:00
// do some insertions
dict.insert(b'A', b'*').expect("dictionary full");
dict.insert(b'B', b'/').expect("dictionary full");
// do some lookups
let key = b'A';
let value = dict[&key]; // the key needs to be passed by reference
2022-01-07 16:24:21 +00:00
defmt::println!("{} -> {}", key, value);
2020-06-22 13:23:59 +00:00
// more readable
2022-01-07 16:24:21 +00:00
defmt::println!("{:?} -> {:?}", key as char, value as char);
2020-06-22 13:23:59 +00:00
// TODO try another insertion
// TODO try looking up a key not contained in the dictionary
// TODO try changing the capacity
dk::exit()
}