Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Improve from_into.rs#2282

Unanswered
jeffrey-cochran asked this question inQ&A
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 teststest_bad_age,test_missing_age, andtest_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 arewrong, 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 freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
1 participant
@jeffrey-cochran

[8]ページ先頭

©2009-2025 Movatter.jp