Skip to content

Chapter 8 "cutr": I'm confused why we're using Option::map and Option::transpose #23

@atc0005

Description

@atc0005

@kyclark,

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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions