Hacker News new | ask | show | jobs
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.

2 comments

This is a great suggestion! I'm on mobile right now, but I'll file an issue when I get home, or if you feel like it, please do!
Please don't quote with code blocks.
Umm...If I don't quote the entire code snippet, I'll end up with just the indented lines being quoted...the joys of HN formatting.
I was speaking with reference to "It would be nice..." which you quoted using a pre/code block (prepending four spaces). Please only use that for actual code, and use `>` at the start of the line to signify a quote, like so:

> Umm...If I don't quote the entire code snippet, I'll end up with just the indented lines being quoted...the joys of HN formatting.

This allows for responsive formatting on different screen sizes, and is especially beneficial for mobile users.