Rust - protein-translation: Use a Result<> and Error type

The protein translation exercise has 2 scenarios where it can “fail”:

  • When an unknown codon is encountered
  • When a “codon” of partial length is encountered

Both these scenarios unfortunately have the same error string in canonical-data.

Currently this is represented with an Option:

pub fn translate(rna: &str) -> Option<Vec<&str>>

It would make more sense, I think, if it returned a Result with an enum Error with an InvalidCodon member. Many other exercises also follow this pattern, and this is a medium exercise so the student should know about Result<> by now.

Let me know what you think, or if an Option was picked for a reason I’m not seeing. It might not be worth breaking past solutions.

Link to the exercise for convenience: Protein Translation in Rust on Exercism

It might make more sense if the goal is to write perfect production code. But I prefer to ask the question “What will the students learn from it?” In this case, I don’t see a benefit. Like you said, there are already a bunch of exercises using the error enum pattern, so students aren’t learning anything new. Rust developers (me included) have a tendency towards perfectionism and we should be careful not to turn every exercise into a mindless error handling chore.

You know what, that’s a really good point. I take back what I said.