News for hackers. Stuff that matters.
Re:From Rust to TypeScript by flowerlad on Saturday September 12, 2020 @12:05PM (#24453420)
In your example if bar() returned an error your program still needlessly calls baz() correct? This is why exceptions are better.
Re:From Rust to TypeScript by dilap on Saturday September 12, 2020 @12:08PM (#24453445)
Nope, the question mark operator is syntax sugar for the "if error, return it" dance.

(Cf. https://doc.rust-lang.org/edition-guide/rust-2018/error-hand...)

Re:From Rust to TypeScript by estebank on Saturday September 12, 2020 @03:10PM (#24454692)
That code desugars to the following

  fn foo() -> Result<int, SomeError> {
     let result = match bar() {
         Ok(val) => val,
         Err(err) => return Err(err.into()),
     };
     let result2 = match baz()  {
         Ok(val) => val,
         Err(err) => return Err(err.into()),
     };
     let result3 = match bazz() {
         Ok(val) => val,
         Err(err) => return Err(err.into()),
     };
     Ok(result + result2 + result3)
  }
Re:From Rust to TypeScript by ben-schaaf on Saturday September 12, 2020 @12:11PM (#24453470)
Nope, it returns an error if bar returns an error - immediately. It's essentially shorthand for `if x.is_none() return X;`