Skip to content

nucleotide-count has no template #512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
BartMassey opened this issue Apr 19, 2018 · 3 comments · Fixed by #565
Closed

nucleotide-count has no template #512

BartMassey opened this issue Apr 19, 2018 · 3 comments · Fixed by #565

Comments

@BartMassey
Copy link

nucleotide/src/lib.rs should be

use std::collections::HashMap;

/// Count the number of occurrences of each of the four nucleotides
/// G, A, C, T.
pub fn nucleotide_counts(dna: &str) -> Result<HashMap<char, usize>, ()> {
    unimplemented!("Count the occurrences of G, A, C, T in {}.", dna);
}

/// Count the number of occurrences of a given nucleotide,
/// which must be either G, A, C or T.
pub fn count(nuc: char, dna: &str) -> Result<usize, ()> {
    unimplemented!("Count the occurrences of {} in {}.", nuc, dna);
}

Also, these should arguably return Option instead of Result, or just panic!() with a failed assert!() on bad input.

@petertseng
Copy link
Member

petertseng commented Apr 19, 2018

As #444 indicates, using () for the error is not preferred, so let's find a better alternative. I'm down with Option, or having the err indicate an invalid character. Undefined exactly which invalid character out of multiple, but as long as we only have one unique invalid character in our inputs, we'll still be able to test for it.

That means I'd be down w/ Result<HashMap<char, usize>, char> and Result<usize, char> if desired.

@BartMassey
Copy link
Author

If you're not down with having the functions be partial, I think Option is the best alternative. I don't think the Err return is going to be that useful in practice, as reflected by its lack of use in the tests.

@petertseng
Copy link
Member

having the functions be partial

If you are willing to share your opinions, that would be a chance to influence policy. I imagine most of the exercises were made when https://doc.rust-lang.org/book/first-edition/error-handling.html was the only edition of the book. It barely mentioned panics. To be fair, it did mention in https://doc.rust-lang.org/book/first-edition/error-handling.html#a-brief-interlude-unwrapping-isnt-evil that panicking should indicate a bug in the program.

These days there's https://doc.rust-lang.org/book/second-edition/ch09-00-error-handling.html and various subsections

For example, perhaps the sentence is relevant:

If someone calls your code and passes in values that don’t make sense, the best choice might be to panic! and alert the person using your library to the bug in their code

If this is to be the case, when would be a situation where the Rust track would want to use Result or Option in a non-contrived way, then?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants