I recently spent about 2hrs to learn rust-lang by following this guide - The Rust Programming Language.
I wrote down some notes and thoughts about the experiences.
This is the first part.
Install Rust on macOS X
$ curl –proto ‘=https’ –tlsv1.2 https://sh.rustup.rs -sSf | sh
The installation process was quite nice. Everything was quite smooth and fast ⚡️.
Not quite sure if it was because I chose
1) which is the default installation.
In addition, the installation comes with local documentation which is really nice 👍.
$ rustup doc
Rust is an ahead-of-time compiled language, meaning you can compile a program and give the executable to someone else, and they can run it even without having Rust installed.
The first compilation took a bit of time, the following compilations were faster, even with the executable deleted from disk. The compilation must be incremental with some caching.
The installation comes with
Cargo is Rust’s build system and package manager.
$ cargo new hello_cargo
$ cd hello_cargo
You’ll see that Cargo has generated two files and one directory for us: a
Cargo.tomlfile and a src directory with a
It has also initialized a new Git repository along with a .gitignore file. Git files won’t be generated if you run cargo new within an existing Git repository; you can override this behavior by using cargo new –vcs=git.
As you can see the
hello_cargo doesn’t have the git files because the folder is in side the repo of this notes.
hello_cargo_vcs has the git files generated.
$ cargo new hello_cargo_vcs --vcs=git
note: I had to delete the
./git files otherwise git returns an error when adding files.
$ git add .
error: 'ch1/hello_cargo_vcs/' does not have a commit checked out
fatal: adding files failed
$ cargo build
Running cargo build for the first time also causes Cargo to create a new file at the top level: Cargo.lock. This file keeps track of the exact versions of dependencies in your project.
build + run
$ cargo run
Cargo figured out that the files hadn’t changed, so it just ran the binary. If you had modified your source code, Cargo would have rebuilt the project before running it,
Compile but doesn’t produce executable which makes this process faster.
$ cargo check
cargo check is much faster than cargo build, because it skips the step of producing an executable. If you’re continually checking your work while writing the code, using cargo check will speed up the process! As such, many Rustaceans run cargo check periodically as they write their program to make sure it compiles. Then they run cargo build when they’re ready to use the executable.
build: produces executable. check: doesn’t produce executable.
Let’s recap what we’ve learned so far about Cargo:
- We can build a project using cargo build.
- We can build and run a project in one step using cargo run.
- We can build a project without producing a binary to check for errors using cargo check.
- Instead of saving the result of the build in the same directory as our code, Cargo stores it in the target/debug directory.
An additional advantage of using Cargo is that the commands are the same no matter which operating system you’re working on.
cargo build --release to compile it with optimizations.
This command will create an executable in target/release instead of target/debug. The optimizations make your Rust code run faster, but turning them on lengthens the time it takes for your program to compile. This is why there are two different profiles: one for development, when you want to rebuild quickly and often, and another for building the final program you’ll give to a user that won’t be rebuilt repeatedly and that will run as fast as possible. If you’re benchmarking your code’s running time, be sure to run cargo build –release and benchmark with the executable in target/release.