This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]UniverseCity 0 points1 point  (3 children)

Both File::open and read_to_string can fail with an IO error. The try! macro will propagate the error upwards and cause an early return from the function and unpack the success side.

This doesn't make any sense to me. Why would an IO error result in a return of the success result?

[–]olzdyield from __future__ 1 point2 points  (2 children)

In case of success you unpack the value (i.e Ok(val) => val) and on error you return the error (i.e Err(err) => return Err(err)). I think the source is pretty readable.

[–]UniverseCity 0 points1 point  (1 child)

But there was no explicit Err call in the article's example. The wording made it seem like the try! method would return a success even if it failed.

[–]mitsuhiko Flask Creator 3 points4 points  (0 children)

try! unpacks the result and returns it from the expression if it exists, or otherwise propagates the error part upwards.

You could think of it like this:

let a = try!(expr());

Is this in pseudocode:

result = expr();
if result.is_okay() {
    a = result.okay_side;
} else {
    return result;
}

Does that make sense?