3 ms·
I thought try! and unwrap() just panic on failure? Isn't that exactly the same thing as `res, _ := somefunc()`?
by weberc2 10y ago
I thought try! and unwrap() just panic on failure? Isn't that exactly the same thing as `res, _ := somefunc()`?
- PeCaN 10y agounwrap panics on failure; try! is equivalent to match somefunc() { Ok(res) => res, Err(e) => return Err(e) } in Rust; or res, err := somefunc() if err != nil { return err } in Go. Also, while I don't really know Go, doesn't res, _ := somefunc() silently ignore the error instead of panicking?
- fizzbatter 10y ago> silently ignore the error instead of panicking? Correct, with the minor note that it of course depends on your usage. If `res` is a pointer, any usage of it will panic because you ignored the err
- __s 10y agoWhich means if you don't use that pointer for awhile you're going to have to do some backtracking to that ignored error
- giovannibajo1 10y agoWhich is why nobody writes that in production code; it's only used in examples and maybe tests
- fizzbatter 10y agoMy Rust on `try!` is rusty... no pun intended, iirc it is the equivalent of: if err := someFn(); err != nil { return err } Where as `unwrap()` is: if err := someFn(); err != nil { panic(err) } So a little dif than your example, fwiw. (Not agreeing or disagreeing with any of the statements, merely commenting on your question)
- justinsaccount 10y agounwrap is also kind of neat in that it 'unwraps' the value, so something that converts strings to ints intval = convert("42").unwrap() is like if intval, err := convert("42"); err != nil { panic(err) } but you can not do intval = convert("42") and pretend it can't fail, but in go you can do intval, _ := convert("42") the rust unwrap would most similar to a intval := mustConvert("42") in go, but 'must' type functions don't exist for every method in every library.