diff --git a/examples/16_traits/2_display_temperature.rs b/examples/16_traits/2_display_temperature.rs index 3e89bf4..bd408ed 100644 --- a/examples/16_traits/2_display_temperature.rs +++ b/examples/16_traits/2_display_temperature.rs @@ -10,9 +10,6 @@ struct Temperature { /// - `Temperature { celsius: 21.5 }` → `"21.5°C"` /// - `Temperature { celsius: -3.0 }` → `"-3.0°C"` /// - `Temperature { celsius: 100.0 }` → `"100.0°C"` -/// -/// Hint: `write!(f, "{:.1}°C", self.celsius)` does the whole job. -/// The `:.1` is the same format specifier you'd use in `println!`. impl fmt::Display for Temperature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { todo!() diff --git a/examples/21_environment_file_parser/4_get_var.md b/examples/21_environment_file_parser/4_get_var.md index 814b2e7..eab225d 100644 --- a/examples/21_environment_file_parser/4_get_var.md +++ b/examples/21_environment_file_parser/4_get_var.md @@ -3,14 +3,12 @@ Configuration values are stored as strings, but consumers want `u16` ports, `bool` flags, and so on. Rather than write one helper per type, declare a generic function bounded by `FromStr` and let the caller pick the type at the call site with a turbofish or a type annotation. -Inside the body, `env.get(key)?` short-circuits on a missing key and `.parse().ok()` collapses the parse `Result` into an `Option`. -Don't try to `?` the parse: `T::Err` is unconstrained here and would need an extra `From` bound. +Return `None` both when the key is missing and when its value cannot be parsed as the requested type. +The standard library methods below provide the pieces; your task is to combine them. ## Useful from the standard library - [`HashMap::get`](https://doc.rust-lang.org/std/collections/struct.HashMap.html#method.get) returns `Option<&String>`. - The `?` propagates the missing-key case as `None`. - [`str::parse`](https://doc.rust-lang.org/std/primitive.str.html#method.parse) uses [`FromStr`](https://doc.rust-lang.org/std/str/trait.FromStr.html) to produce `Result`. That's the trait the `where` clause is asking for. -- [`Result::ok`](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok) drops the error and yields `Option`, exactly the function's return type. -- The body fits on one line: `env.get(key)?.parse().ok()`. +- [`Result::ok`](https://doc.rust-lang.org/std/result/enum.Result.html#method.ok) drops the error and yields `Option`, matching the function's return type. diff --git a/examples/21_environment_file_parser/4_get_var.rs b/examples/21_environment_file_parser/4_get_var.rs index 78f8f01..19eb17c 100644 --- a/examples/21_environment_file_parser/4_get_var.rs +++ b/examples/21_environment_file_parser/4_get_var.rs @@ -2,12 +2,6 @@ use std::collections::HashMap; /// Gets an environment variable with type conversion. /// Parses the string value into the requested type. -/// -/// Hint: the natural solution is `env.get(key)?.parse().ok()`. Don't try to -/// `?` the parse: `T::Err` is unconstrained here, so `?` would need a -/// `From` bound that we haven't added. `.ok()` collapses -/// `Result` into `Option`, which is what the signature -/// returns anyway. fn get_env_var(env: &HashMap, key: &str) -> Option where T: std::str::FromStr, diff --git a/solutions/16_traits/2_display_temperature.rs b/solutions/16_traits/2_display_temperature.rs index 3808f9d..8c8e7f2 100644 --- a/solutions/16_traits/2_display_temperature.rs +++ b/solutions/16_traits/2_display_temperature.rs @@ -10,9 +10,6 @@ struct Temperature { /// - `Temperature { celsius: 21.5 }` → `"21.5°C"` /// - `Temperature { celsius: -3.0 }` → `"-3.0°C"` /// - `Temperature { celsius: 100.0 }` → `"100.0°C"` -/// -/// Hint: `write!(f, "{:.1}°C", self.celsius)` does the whole job. -/// The `:.1` is the same format specifier you'd use in `println!`. impl fmt::Display for Temperature { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{:.1}°C", self.celsius) diff --git a/solutions/21_environment_file_parser/4_get_var.rs b/solutions/21_environment_file_parser/4_get_var.rs index 3f0e876..1ba8a4d 100644 --- a/solutions/21_environment_file_parser/4_get_var.rs +++ b/solutions/21_environment_file_parser/4_get_var.rs @@ -2,12 +2,6 @@ use std::collections::HashMap; /// Gets an environment variable with type conversion. /// Parses the string value into the requested type. -/// -/// Hint: the natural solution is `env.get(key)?.parse().ok()`. Don't try to -/// `?` the parse: `T::Err` is unconstrained here, so `?` would need a -/// `From` bound that we haven't added. `.ok()` collapses -/// `Result` into `Option`, which is what the signature -/// returns anyway. fn get_env_var(env: &HashMap, key: &str) -> Option where T: std::str::FromStr,