Description
https://github.com/exercism/rust/blob/master/exercises/wordy/tests/wordy.rs#L106 I will reproduce here for convenience:
assert!(WordProblem::new(command).answer().is_none());
However, at the time of parsing command
, we can know whether the command is valid. We don't need to wait until calling answer()
.
The reasons for solving this problem, regardless of what way is used to solve it, are that it's a better interface to fail as soon as possible when you know you will fail. It's harder on consumers of an interface if they have to trace back their own actions to figure out exactly where in the past they went wrong.
A possible solution would be to simplify the interface to:
answer_word_problem(command: &str) -> Option<i32>
I suggest such a simplification, similar to same simplification done for brackets
in #620
The additional reason for the simplification as the particular way to solve this problem would be to not force the creation of a struct where it is not necessary.