
Getting started with Rust by building a tiny Markdown compiler - jesselawson
https://jesselawson.org/rust/getting-started-with-rust-by-building-a-tiny-markdown-compiler/
======
AlchemistCamp
I'm not completly new to Rust, but for some reason writing a Markdown has
become a routine project I do in every language I'm learning.

This tutorial looks fantastic. Is the layout based on a 3rd-party theme or
template? I really like the presentation.

~~~
satvikpendem
Thanks for your Elixir tutorials!

For those who don't know, Alchemist Camp is a YouTube channel and website for
learning Elixir: [https://alchemist.camp](https://alchemist.camp)

~~~
jesselawson
Wonderful, thank you!

------
berkus
Hmm, I see the author liberally using _filename as a variable name saying "I
just use names with underscores as functions arguments names".

However: In Rust, variables starting with underscores are usually indicating
unused ids and if you actually forget to use it - you will not get a warning.
As a tutorial material this sets very wrong mindset for the beginners. ONLY
start variable with an underscore if you mean to not use it.

@jesselawson - please update this.

~~~
jesselawson
Thanks, berkus. I've added an [issue]([https://github.com/jesselawson/rust-
tiny-markdown-compiler-t...](https://github.com/jesselawson/rust-tiny-
markdown-compiler-tutorial/issues/12)) to track these valid concerns. I
appreciate your help!

------
gilmi
I think the author did a great job with the format of this tutorial!

It includes all the steps needed to write the app, has exercises, is neatly
splitted to chapters with clear expectations and summaries for each chapter
and so on. Everything needed to reduce frustration, maximize knowledge
retention, and make it a nice experience as possible to the reader. Awesome
work!

~~~
jesselawson
Thank you for taking the time to share that. I appreciate your feedback. :)

------
einpoklum
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?

~~~
aaronjanse
> 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).

~~~
einpoklum
A little counter-intuitive IMHO. But - I live with C++'s template voodoo, so I
guess I can't really complain.

~~~
Measter
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](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](https://doc.rust-
lang.org/std/panic/fn.catch_unwind.html)

------
greglindahl
I'm a bit confused by the use of the word "compiler" to refer to something
that turns markdown into html.

~~~
ChuckMcM
Perhaps 'transpiler' would be clearer? As many consider an HTML page to be an
'executable' (which is run by a browser) converting a text file into an
executable would meet the CS definition. Pedantically the process of
"compiling" markdown is more akin to "typesetting" but using the phrase
'markdown typesetter' would be even more obscure.

If there a word for the action of converting the description of a ritual to
the actual practice of that ritual, that would provide a suitable root word
form here. That said, using the closest example I can think of, music, the
root word there is 'performing' when the music is performed and recorded when
the music is performed into a storage medium, and transposed when the music is
converted from one description to another description while maintaining the
overall musical relationship between the timbres.

It is that latter version, transposition, which suggests the word 'transpile'.

~~~
greglindahl
I once worked on a source-to-source translator from C++ to C. In that era,
that was the common term for that kind of software. I think that's more clear
than "transpiler".

However, what we're talking about here is closer to TeX than cfront-to-c or
even TypeScript-to-Javascript.

------
mrlonglong
I tried Rustlings, found it a wee bit too easy to do, but I'm not quite
finished yet.

------
Lapland
Looks great, thanks for creating this!

~~~
jesselawson
Thank you!

