100-exercises-to-learn-rust icon indicating copy to clipboard operation
100-exercises-to-learn-rust copied to clipboard

(05) ticket_v2 - (07) unwrap has nothing (necessarily) to do with unwrapping

Open shblack opened this issue 5 months ago • 1 comments

This one seems completely disconnected from the parallel text. Unwrapping is not necessarily involved, at all, in solving the problem. It's not even particularly suggested by the way the problem is presented.

// TODO: `easy_ticket` should panic when the title is invalid.
//   When the description is invalid, instead, it should use a default description:
//   "Description not provided".
fn easy_ticket(title: String, description: String, status: Status) -> Ticket {
    // Validate title and panic if invalid
    if title.is_empty() {
        panic!("Title cannot be empty");
    }
    if title.len() > 50 {
        panic!("Title cannot be longer than 50 bytes");
    }

    // Use default description if description is invalid
    let final_description = if description.is_empty() || description.len() > 500 {
        "Description not provided".to_string()
    } else {
        description
    };

    Ticket {
        title,
        description: final_description,
        status,
    }
}

shblack avatar Jun 24 '25 15:06 shblack