3 ms·
The Rust code you have isn't idiomatic. It's gar more likely to see: let greeting_file = File::open("hello.txt")?l vs file, err := os.Open("hello.txt
by nemothekid 3y ago
The Rust code you have isn't idiomatic. It's gar more likely to see:
let greeting_file = File::open("hello.txt")?l
vs
file, err := os.Open("hello.txt")
if err != nil {
return fmt.errorf("faild to open hello; %w, err)
}
However, I would argue the distinction isn't just cosmetic. The compiler prevents me from not checking the error and blowing up the application with a nil pointer exception.
The TFA even capes this pattern:
type Result[T any] struct {
Value T
Error error
}
and I wonder if we will start to see it more now that generics are in Go.
- philozzzozzz 3y agoWhere is the error handling in your Rust code?
- kinghajj 3y agoThe `?` at the end of the Rust example expands into the equivalent of the Go `if err != nil` idiom.
- philozzzozzz 3y agoYou are illustrating the point beautifully.
- dlisboa 3y agoNot a Rust programmer, but I took the Rust code from the Rust book. Admittedly I didn’t read the whole chapter, perhaps they talk idioms later.
- lolinder 3y agoYou just needed to go down a bit further [0]. They understandably didn't want to introduce syntactic sugar until after they'd introduced the syntax it desugars into. [0] https://doc.rust-lang.org/book/ch09-02-recoverable-errors-with-result.html#a-shortcut-for-propagating-errors-the--operator https://doc.rust-lang.org/book/ch09-02-recoverable-errors-wi...