-
Notifications
You must be signed in to change notification settings - Fork 303
Description
First, many thanks for writing this book and your ongoing support of it. I'm working through the 2024 edition and am learning a lot.
I'm in chapter 8 on page 182-183 and am reviewing how you incorporated the parse_pos function into the run function.
I understand what this code block is doing:
command-line-rust/08_cutr/src/main.rs
Lines 69 to 84 in 10d983f
| let extract = if let Some(fields) = | |
| args.extract.fields.map(parse_pos).transpose()? | |
| { | |
| Extract::Fields(fields) | |
| } else if let Some(bytes) = | |
| args.extract.bytes.map(parse_pos).transpose()? | |
| { | |
| Extract::Bytes(bytes) | |
| } else if let Some(chars) = | |
| args.extract.chars.map(parse_pos).transpose()? | |
| { | |
| Extract::Chars(chars) | |
| } else { | |
| unreachable!("Must have --fields, --bytes, or --chars"); | |
| }; | |
but I don't understand why we wouldn't use an approach like this instead:
let extract: Extract = if let Some(fields) = args.extract.fields {
Extract::Bytes(parse_pos(fields)?)
} else if let Some(bytes) = args.extract.bytes {
Extract::Chars(parse_pos(bytes)?)
} else if let Some(chars) = args.extract.chars {
Extract::Fields(parse_pos(chars)?)
} else {
unreachable!("Must have --fields, --bytes, or --chars");
};I feel like I'm missing something. Does your implementation handle a scenario I'm missing, is it more idiomatic, are you just introducing your readers to additional corners of Rust that they might not encounter otherwise?
Again, many thanks for the book and thanks in advance for any insights you can share.