Time for an adventure! No idea why, but felt like trying out Rust.

I'm following their getting started doc.

Local or hosted?

First option it gives me is to mess around in the online playground or set up Rust on my machine. I'm going to opt for setting it up locally. Always interesting to see how complicated that is.

The site recognizes my OS and gives me the command I need to run, pretty nice!

A quick setup prompt and I'm good to go. It added everything to my $PATH which is great, but I did need to run source $HOME/.cargo/env or otherwise reboot my terminal.

Create a project

Looks like cargo is the Rust version of npm. I can create a new project with cargo new and run it with cargo run.

I do both and I've got a working Rust project locally. That was under 5 minutes, very impressive.

Now things get interesting

I have a general rule when working through tutorials that I follow the spirit of them rather than the law. The first thing this walkthrough recommends I do is use cargo to download a dependency.

While the tutorial recommends ferris-says I'm going to pick something different from the linked crates.io directory. Same instructions, but no copy/paste. This helps me think through what I'm actually doing. I'm going for rand, math major in me can't resist.

Cargo.tml looks to be package.json and packages are called crates. So far so good.

This is interesting. I'm not used to toml structure at all. Almost looks like markdown in some ways.

rand = "0.7.3"

Using the dependency

Looking at the docs things seem decently 1:1 with JS. First I need to import stuff which in Rust is use. I'm going to use the random function.

use rand::random;

Just using random() results in a boolean, so I actually need to give it something to randomly select from.

println!("Print a random num: {}", random::<u8>());

Note that I can reference the function name directly. I found some examples with rand::random::<u8>() but with the use statement I have above that's not necessary. Appears to work the same way named imports do.

The whole thing

All together this is my program.

use rand::random;
fn main() {
println!("Print a random num: {}", random::<u8>());

Note that println needs to be wrapped in the main function. I didn't have that at first and saw the error main function not found in crate hello_rust.

Before I run the project again I need to run cargo build to handle the dependency management. So far I've only referenced the package in Cargo.toml rather than install it.

After that I'm able to run cargo run and I see the following output in my terminal.

Print a random num: 33

Sweet! A working program.

What next?

Looks like that's the end of the tutorial, at least based on my substitutions. First impressions are pretty positive. Error messages were useful and descriptive. Docs were approachable and easy to find.

Chris Biscardi has some Rust egghead videos so I think I'll head there next! It's over an hour of content so I'll save that for the next post.