Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions examples/16_traits/2_display_temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!()
Expand Down
8 changes: 3 additions & 5 deletions examples/21_environment_file_parser/4_get_var.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<T, T::Err>`.
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<T>`, 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<T>`, matching the function's return type.
6 changes: 0 additions & 6 deletions examples/21_environment_file_parser/4_get_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T::Err>` bound that we haven't added. `.ok()` collapses
/// `Result<T, T::Err>` into `Option<T>`, which is what the signature
/// returns anyway.
fn get_env_var<T>(env: &HashMap<String, String>, key: &str) -> Option<T>
where
T: std::str::FromStr,
Expand Down
3 changes: 0 additions & 3 deletions solutions/16_traits/2_display_temperature.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 0 additions & 6 deletions solutions/21_environment_file_parser/4_get_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T::Err>` bound that we haven't added. `.ok()` collapses
/// `Result<T, T::Err>` into `Option<T>`, which is what the signature
/// returns anyway.
fn get_env_var<T>(env: &HashMap<String, String>, key: &str) -> Option<T>
where
T: std::str::FromStr,
Expand Down
Loading