From e0f9f6be4276d926aa9cef76f2dc407063a4f435 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 22 Mar 2023 20:19:16 +0200 Subject: we have a Robert! Sponsored-by: author --- src/main.rs | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main.rs b/src/main.rs index 0bf2c9f..c229269 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +#[derive(Debug)] struct Container { values: Vec<(K, V)>, } @@ -12,12 +13,13 @@ impl Container { } fn get(&self, k: &K) -> Option<&V> { + let mut value = None; for (actual_k, v) in self.values.iter() { if actual_k == k { - return Some(&v); + value = Some(v); } } - None + value } } @@ -28,6 +30,7 @@ fn main() { let mut cont = Container::new(); cont.insert(bob.clone(), bob.clone()); cont.insert(bob.clone(), robert); + println!("values: {:?}", cont); println!("{} -> {:?}", &alice, cont.get(&alice)); println!("{} -> {:?}", &bob, cont.get(&bob)); // Output should be Robert -- cgit v1.2.1