In Go when you run a lot of things it returns an error variable for you to take care of or disregard to handle all of your stuff.
err, otherVariable := someFunction()
if err != Nil {
// something
}
But in rust it has throwing an exception built in with .except() which is kinda basic but interesting and a lot cleaner looking for larger bits of code
let token = env::var("TESTING_DISCORD_TOKEN")
.expect("Expected a token in the environment");
So this prints out "Expected a token in the environment" if it errors out so kinda nice.
There is defiantly better ways of handling errors in both languages but this is just a perspective from a beginner in both
Top comments (3)
.expect()(or.unwrap()) are not error handling mechanisms, and certainly not equivalent toif err != nilin Go. The usual error handling mechanism in Rust is the?operators, that has the same meaning asif err == nil { return; }.Would that be done like this?:
Or
match.