radio-puzzle.rs: simplify error handling, add explanatory comments

This commit is contained in:
Lotte Steenbrink 2020-06-17 14:30:07 +02:00
parent 6902a5e81b
commit e8c5acfe9a

View file

@ -23,12 +23,11 @@ fn main() -> ! {
packet.copy_from_slice(msg.as_bytes()); packet.copy_from_slice(msg.as_bytes());
radio.send(&packet); radio.send(&packet);
log::info!("sent: {:?}", msg); log::info!("sent: {:?}", msg);
radio.recv(&mut packet).ok(); // listen for a response packet and ensure it is not corrupted
if let Ok(s) = str::from_utf8(&*packet) { if radio.recv(&mut packet).is_ok() {
log::info!("received: {}", s); // convert the packet contents to str or print error message on failure
} else { let response = str::from_utf8(&*packet).expect("could not convert response to str");
log::info!("received: {:?}", &*packet); log::info!("received: {}", response);
} }
dk::exit() dk::exit()
} }