Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Discussion options

I failed these tests initially, but when I looked at them, I realized that a reasonable interpretation of the instructions would produce different results.

Specifically, the tests test_bad_age, test_missing_age, and test_missing_name are not testing the behavior that I inferred from the instructions. I understood the instruction (4) to be "If the name is empty, return [a Person with the default name]" and (6) to be "If parsing the age fails, return [a Person with the default age]".

The reason my brain immediately jumped to this incorrect conclusion is that the context makes it clear that, in such a case, it's possilbe the rest of the information is valid--so why throw away valid information?

I don't think the tests or instructions are wrong, only that they could be clearer (or better).

For reference, here is what my initial implementation looked like:

impl From<&str> for Person {
    fn from(s: &str) -> Self {
        let strs: Vec::<&str> = s.split(',').collect();
        match strs[..] {
            [in_name, in_age] => Person {
                name: match in_name {s if s.is_empty() => Person::default().name, s => s.to_string() },
                age: in_age.parse::<u8>().unwrap_or(Person::default().age)
            },
            _ => Person::default()
        }
    }
}
You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
🙏
Q&A
Labels
None yet
1 participant
Morty Proxy This is a proxified and sanitized view of the page, visit original site.