From e8c5acfe9ab2b7c5819d7613f4ea3a1546787210 Mon Sep 17 00:00:00 2001 From: Lotte Steenbrink Date: Wed, 17 Jun 2020 14:30:07 +0200 Subject: [PATCH] radio-puzzle.rs: simplify error handling, add explanatory comments --- beginner/apps/src/bin/radio-puzzle.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/beginner/apps/src/bin/radio-puzzle.rs b/beginner/apps/src/bin/radio-puzzle.rs index 8b526ec..f3c0174 100644 --- a/beginner/apps/src/bin/radio-puzzle.rs +++ b/beginner/apps/src/bin/radio-puzzle.rs @@ -23,12 +23,11 @@ fn main() -> ! { packet.copy_from_slice(msg.as_bytes()); radio.send(&packet); log::info!("sent: {:?}", msg); - radio.recv(&mut packet).ok(); - if let Ok(s) = str::from_utf8(&*packet) { - log::info!("received: {}", s); - } else { - log::info!("received: {:?}", &*packet); + // listen for a response packet and ensure it is not corrupted + if radio.recv(&mut packet).is_ok() { + // convert the packet contents to str or print error message on failure + let response = str::from_utf8(&*packet).expect("could not convert response to str"); + log::info!("received: {}", response); } - dk::exit() }