Sometime ago I started to learn Rust, first by reading resources like The rust book and Rust by example. Then I continue with the rustlings exercises and now I decided to continue with Rust in motion video course.

This are some notes that I have been taking…

rustup

rustup is The Rust toolchain installer, and we can use it for install, update and switch between rust versions.

To install rustup, run this command in a terminal

1
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Or if you are on window download the rustup-init.exe file from this page

Also, rustup allow us to check the updates and update our rust version

1
2
3
4
5
6
7
8
9
$ rustup check
stable-x86_64-apple-darwin - Update available : 1.42.0 (b8cedc004 2020-03-09) -> 1.44.0 (49cae5576 2020-06-01)

$ rustup update
info: syncing channel updates for 'stable-x86_64-apple-darwin'
info: latest update on 2020-06-04, rust version 1.44.0 (49cae5576 2020-06-01)
...

  stable-x86_64-apple-darwin updated - rustc 1.44.0 (49cae5576 2020-06-01) (from rustc 1.42.0 (b8cedc004 2020-03-09))

Rust support a great number of platforms and allow us to cross-compile to them by adding the target( an a linker if needed ).

For example we can add the webAssembly target by running

1
$ rustup target add wasm32-unknown-unknown

Cargo

Cargo is the package manager of rust ( like npm to node.js )

$ cargo new –bin myproject

$ cargo new –lib myproject

The flags --bin and --lib allow us to crate a new executable project or a new library project ( e.g a crater ).

Cargo also will create the directory with a Cargo.toml file ( like the package.json of node.js and a src directory with a boilerplate code.

1
2
3
4
.
 |-Cargo.toml
 |-src
 | |-main.rs

Cargo also allow us to build ( and install the dependencies ) and run the project

1
2
3
4
5
6
7
8
$ cargo build
   Compiling notes-unit-1 v0.1.0 (/Users/personal/rust/notes-unit-1)
    Finished dev [unoptimized + debuginfo] target(s) in 3.76s

$ cargo run
    Finished dev [unoptimized + debuginfo] target(s) in 0.15s
     Running `target/debug/notes-unit-1`
Hello, world!

Cargo will automatically create the file Cargo.lock to keep track of the deps ( like package-lock.json for node.js ) and a target directory. By default cargo compiles the executable in the debug directory with a very few optimizations and if you want to compile a release version you should run

1
$ cargo build --release

This will generate the executable ( with optimizations ) in the target/release directory.

Also, you can build and run with one command.

1
2
3
4
5
$ cargo run [--release]
   Compiling notes-unit-1 v0.1.0 (/Users/personal/rust/notes-unit-1)
    Finished dev [unoptimized + debuginfo] target(s) in 2.62s
     Running `target/debug/notes-unit-1`
Hello, world!!

This is all for now, next note will be about syntax and I will try to generate and example project…

Thanks!

Note: also published in collected notes/javier