Getting Started
This tutorial series is minified version of The Rust Book:
and follow up of this tutorial series:
So, if you like watching video tutorial, do check out the above series by Bogdan Pshonyak and follow up with the notes here.
Installing Rust on Linux or macOS
curl --proto '=https' --tlsv1.3 https://sh.rustup.rs -sSf | sh
Check Rust version with: rustc --version
Then go ahead and install official Plugin/extensions for Rust and Rust-analyzer for your faviourite IDE.
Hello, World in Rust
mkdir ~/projects
cd ~/projects
mkdir hello_world
touch main.rs
code .
And then write in main.rs
fn main() {
println!("Hello World");
}
Let’s compile this and run this:
rustc main.rs
./main.rs
In real world projects we’re going to work with multiple files with dependencies and therefore we need a build system and package manager: Cargo.
Check it’s version as: cargo --version
Let’s create a new package named hello_cargo
:
Note
cargo new hello_cargo
which build a directory structure something like this:
.
├── Cargo.toml
├── .gitignore
└── src
└── main.rs
The Cargo.toml
contains information about the package, and list dependencies along with their aliases.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
To build using Cargo, let’s cd into our newly created directory: cd hello_cargo
and then:
cargo build
cargo run
This creates new cargo.lock
(don’t temper with this) target
dir which contains bunch of other stuff and main executable file.
Run cargo --help
to see more bunch of options.
Note
cargo check
to check program for errors without actually building an executable file, much faster.The default tools on the rustup toolchain are:
- Cargo: package manager and crate host for Rust
- Rustup: the rust toolchain installer/updater/release switcher
- Rustc: the rust compiler
To update Rust, simply run:
rustup update
and to uninstall:
rustup self uninstall