Rust lang notes
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
| |
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
| |
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
| |
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.
| |
Cargo also allow us to build ( and install the dependencies ) and run the project
| |
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
| |
This will generate the executable ( with optimizations ) in the target/release directory.
Also, you can build and run with one command.
| |
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
Author Javier Viola
LastMod 2020-06-15