News for hackers. Stuff that matters.
Re:Getting started with Rust by building a tiny Markdown com... by einpoklum on Saturday May 16, 2020 @05:44PM (#23207464)
1. This is more of a "taste-teaser" than a tutorial in my opinion, but it's nice enough I guess.

2. Perhaps a different page for each chapter/part would have been nice.

3. I wonder how different would a C++ "taste-teaser" with the same scope and goal be.

4. I'm not a Rustian (Rustic? Rust-guy?); this seems strange to me:

    // Try to open the file
    let file = File::open(&input_filename)
                    .expect("[ ERROR ] Failed to open file!");
why am I expecting an error? Shouldn't that keyword be "on_exception" or something?
Re:Getting started with Rust by building a tiny Markdown com... by JoshTriplett on Saturday May 16, 2020 @08:10PM (#23208274)
> Rustian (Rustic? Rust-guy?);

"Rustacean", pronounced like "crustacean" without the initial 'c'; remember that Rust's logo is a crab (named Ferris).

Also, regarding ".except": like ".unwrap", you almost never want to use it, because it panics on failure. Instead, you can write:

    let file = File::open(&input_filename)?;
That will propagate the error upwards; if you propagate an error out of main, Rust will print an error message and exit with exit code 1, which is often what you want. Some error handling libraries also let you add context to the error before propagating it.
Re:Getting started with Rust by building a tiny Markdown com... by aaronjanse on Saturday May 16, 2020 @05:48PM (#23207490)
> why am I expecting an error? Shouldn't that keyword be "on_exception" or something?

`expect` takes an input that is either an error or a value, then turns that input into just a value. The provided string is the error message if something goes wrong (i.e. the input was an error).

Re:Getting started with Rust by building a tiny Markdown com... by einpoklum on Saturday May 16, 2020 @06:41PM (#23207799)
A little counter-intuitive IMHO. But - I live with C++'s template voodoo, so I guess I can't really complain.
Re:Getting started with Rust by building a tiny Markdown com... by Measter on Sunday May 17, 2020 @08:36AM (#23211527)
The way recoverable errors are signalled in Rust is through the return value, which is done using the `Result` enum. That enum is basically[0] defined like this:

    enum Result<T, E> {
        Ok(T),
        Err(E),
    }
Opening a file can, of course, fail for whatever reason, so `File::open` returns a `Result`. The `expect` method in that example is one defined on `Result` which takes that result and a message string, and if it's the `Ok` variant will return the value stored that variant. If its the `Err` variant it will print the input string and start an unrecoverable [1] panic, ending the program.

You would typically use `expect` (or `unwrap`, which does the same thing but without a custom message) when you either know for certain that it can't fail, or the failure condition is not something the program can recover from.

[0] Full definition can be found at https://doc.rust-lang.org/std/result/enum.Result.html

[1] Technically, you can catch a panic unwind [2], but it's more awkward and intended more for FFI boundaries than normal code. There's also no guarantee that the program will unwind. The compiler might have been set to abort on a panic instead of unwinding, in which case there's no unwind to catch.

[2] https://doc.rust-lang.org/std/panic/fn.catch_unwind.html

Re:Getting started with Rust by building a tiny Markdown com... by worble on Saturday May 16, 2020 @07:52PM (#23208187)
I read as it as "I expect this to work" since expect throws a panic! if it can't unwrap the result. Essentially you're saying you expect this to work because otherwise you'd handle the error properly using pattern matching or something.
Re:Getting started with Rust by building a tiny Markdown com... by sezna on Saturday May 16, 2020 @05:50PM (#23207511)
it is intended to be read as “expect this to work”, or “expect this isn’t an error”
Re:Getting started with Rust by building a tiny Markdown com... by einpoklum on Saturday May 16, 2020 @06:41PM (#23207803)
But the argument is an error message, so it's not "expect this to work"...
Re:Getting started with Rust by building a tiny Markdown com... by azdle on Saturday May 16, 2020 @06:54PM (#23207884)
That's technically the second argument. Just like in C++, when using a chained method, the first argument is implied to be the thing you're chaining on. So this is the same thing as calling Result::expect(File::open(&input_filename), "[ ERROR ] Failed to open file!"), but I've never actually seen someone ever use expect that way.

See also the definition of expect: https://doc.rust-lang.org/std/result/enum.Result.html#method...