|
|
|
|
|
by curun1r
3394 days ago
|
|
Under the "Improving our I/O Project" section, there's this little paragraph: It would be nice if we could use ? on the Option
returned from next, but ? only works with Result
values currently. Even if we could use ? on Option
like we can on Result, the value we would get would
be borrowed, and we want to move the String from
the iterator into Config.
It seems remiss to not mention that Option/Result have combinators/adapters too, no? let search = match args.next() {
Some(arg) => arg,
None => return Err("Didn't get a search string"),
};
becomes: let search = args.next().ok_or("Didn't get a search string")?;
I'm not the most fluent in Rust, so please tell me whether the above two snippets are not equivalent in some way.It might actually be a nice segue into a page on the combinators of Option and Result, since functional-style Rust seems to benefit from liberal use of them. |
|