Hacker News new | ask | show | jobs
by oneshtein 651 days ago
How to return an error in your example?
2 comments

    pub fn read(path: Path) -> Result<Bytes> {
      File::open(path)?.read_to_end()
    }
isn't so bad either.
Exactly, and this is in my experience what most Rust code ends up looking like.

It compromises a bit on generality and (potential) performance to achieve better readability and succinctness. Often a worthwhile trade-off, but not something the standard library can always do.

Throw an exception

proving the point of the article even further

You would actually use a Result type:

  use std::io;
  
  pub fn read(path: Path) -> io::Result<Bytes> {
    File::open(path)?.read_to_end()
  }
Sure, if you are allowed to change the signature, makes it look more ugly than just returning Bytes though