We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
1. Rust and Your Development Environment
Here we will learn what we need to get started with Rust.
Installing Rust
The standard way to install the Rust toolchain on Linux is with rustup:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Then reload your shell:
source "$HOME/.cargo/env"
Check that it worked:
rustc --version
cargo --version
This installs:
-
rustc→ the Rust compiler -
cargo→ Rust package/build tool -
rustup→ toolchain manager
You can also install specific toolchains later, for example:
rustup toolchain install stable
rustup toolchain install nightly
And switch defaults with:
rustup default stable
Testing Rust with, “Hello, World”
1. Create a new directory in which to place your Rust projects.
2. Open a command prompt or terminal.
3. Navigate to the Rust project directory you created with cd. For example, cd Rust.
4. Type- cargo new testrust -ENTER .
5. Change to the new testrust directory by typing cd testrust ENTER .
6. Run your new program by typing cargo run ENTER .
Updating Rust
rust check
rustup update
Installing and Configuring Your Development Environment
• On the low end, Kate, Notepad++, and GEdit are all quite usable with
Rust. You don’t get much language integration beyond syntax highlighting.
• EMACS and Vim/Neovim can both integrate with Rust Analyzer, the Rust
Language Server, and debugging tools.
• JetBrains makes CLion and IntelliJ, both of which integrate well with Rust.
• Microsoft’s Visual Studio Code, used alongside the Rust Analyzer, CodeLLDB
plugins, works very well for Rust.
• Sublime Text has Rust integration available.
Managing Projects with Cargo
cargo is similar to mix in the Elixir world. It is used to help Rust developers when the make crates.
Starting a New Project with Cargo
Every new project begins as an empty crate. To create a new project:
1. Open a terminal/command prompt.
2. Navigate to the home directory you selected for your Rust code (using cd).
3. Don’t create a new subdirectory for your project—Cargo will do that for you.
4. Type cargo new [project name].
5. Rust has created a subdirectory named [project name]. Enter it with cd [project
name].
For example, to make a project named “Hello” in my preferred rust directory,
I’ll use:
cd c:\users\herbert\rust➾
cargo new hello➾
Created binary (application) `hello` package
Run Hello, World
cargo run
If you would like to not use git you can run this command.
cargo new --vcs=none [project name]
Creating Your First Rust Program
We ran the Hello World cargo creation project so let’s take a second to look over the project.
Cargo Metadata
Open the Cargo.toml file within the “Hello” project
[package]
name = "testrust"
version = "0.1.0"
edition = "2024"
[dependencies]
This uses the TOML [Tom’s Obvious, Minimal Language]. The [package] part of the code is there to describe your project. Here are some of the basic needs of the structure of the [package]
• name: the name of your program—in this case “Hello”. It defaults to the
project name you provided when you called cargo new. When you compile
your program, the name will be used as the name of your program. On
Windows, hello becomes hello.exe. On UNIX-derived systems, it will be named
hello.
• version: the version of your project. Cargo will start you at 0.1.0. You only
need to update the version number when you publish a new version of
your crate. Otherwise, you may update the version number when you feel
it helpful to indicate that significant progress has been made. You’ll learn
about Rust semantic versioning in Package Management with Cargo, on
page 17. For now, stick to version 0.x.y. You can go above 10 on each
number—0.10.0 is fine.
• authors is a list, denoted by the square braces. It may contain a comma-
separated list of author names in quotation marks. If you have git setup,
it automatically pulls your name and email address from there.
• edition: the major edition of Rust used by the project. It will always default
to the current edition—2018—at the time of writing. Editions are allowed
to make substantial changes to Rust syntax and may break older pro-
grams. Specifying the edition tells the Rust compiler which set of syntax
rules it may use.
Hello World
Now let’s open the src/main.rs this is the minimum needed to run the simple line “Hello World.”
fn main() {
println!("Hello, world!");
}
Rust uses {} the denote scopes. A scope represents a block of code that runs together.
Printing Text
println!("Hello World!");
The exclamation mark denotes a macro. Rust macro system is very powerful- and allows for syntax that wouldn’t work with regular expressions. There will be extended syntax options for the current line of code but we will get into that later.
“Hello World!” is a string literal. Any text can be put into it just know that it is a literal string.
Using Cargo to Build, Check, or Run Your Project
You can type cargo help for a full list of all the commands or cargo [command] help to get more an the specific command.
• Quickly check to see if a project is valid by typing cargo check. This checks
your project and its dependencies for basic structural errors. This is typically
a lot faster than a full build.
• Compile—but do not run—your project with cargo build.
• Remove the entire target directory (into which compiled code is placed)
with cargo clean
Debug and Release Builds
We compiled the program in the debug mode with the standard cargo build or cargo run, this allows us to better debug and have an easier time seeing the issues in the code.
cargo run –release builds with optimizations enabled. It is faster at runtime, but takes longer to compile and is less convenient for debugging
Formatting Your Code
There is a built-in formatter in Rust and it is run by cargo fmt
Finding Common Mistakes with Clippy
There is a built-in helper in Rust called Clippy. At any time in your programming you can type cargo clippy to get some help for your project. Let’s create a new clippy project and see how it works by adding this code to the src/main.rs
fn main() {
let MYLIST = ["One", "Two", "Three"];
for i in 0..3 {
println!("{}", MYLIST[i]);
}
}
Then we can run the program and we will get some helpful hints.
warning: variable `MYLIST` should have a snake case name
--> src/main.rs:2:9
|
2 | let MYLIST = ["One", "Two", "Three"];
| ^^^^^^ help: convert the identifier to snake case: `mylist`
|
= note: `#[warn(non_snake_case)]` (part of `#[warn(nonstandard_style)]`) on by default
warning: the loop variable `i` is only used to index `MYLIST`
--> src/main.rs:3:14
|
3 | for i in 0..3 {
| ^^^^
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/rust-1.95.0/index.html#needless_range_loop
= note: `#[warn(clippy::needless_range_loop)]` on by default
help: consider using an iterator
|
3 - for i in 0..3 {
3 + for <item> in &MYLIST {
|
warning: `clippy` (bin "clippy") generated 1 warning
Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.36s
Running `target/debug/clippy`
One
Two
Three
Let’s talk about those warnings. One is a simple snake case so MYLIST should be my_list. The other is that Rust has a built-in iterator system. That is the ranged-for loop. Clippy showed us how to fix it.
for i in 0..3
# ->
for num in &my_list
Making Clippy Pedantic
You can make Clippy very pedantic. Add this line to the main.rs file.
#![warn(clippy::all, clippy::pedantic)]
Package Management with Cargo
Cargo can install dependencies for you. Here are a few commands that can work to install and search for dependencies.
cargo search bracket-terminal
bracket-terminal = "0.8.7" # ASCII/Codepage 437 terminal emulator with a game loop. Defaults to OpenGL, also support W…
bracket-state-machine = "0.2.0" # State management library for bracket-lib terminal
bracket-rex = "0.8.7" # Load/save REX Paint files and utilize them in bracket-terminal projects.
rltk = "0.8.7" # A CP437/ASCII terminal library and helpers to make creating roguelike games in Rust easy.…
bracket-lib = "0.8.7" # Meta-crate holding the entirety of bracket-lib (and exposing it). Use this for the full r…
bracket-color = "0.8.7" # RGB and HSV color handling and utilities, including lerp and W3C named colors. Part of th…
ratatui-bracket-terminal = "0.2.0" # Ratatui backends for Bracket Terminal
note: to learn more about a package, run `cargo info <name>`
cargo search slotmap
slotmap = "1.1.1" # Slotmap data structure
slotmap_fork_lmondada = "1.0.8" # Slotmap data structure
rust-lockless-slotmap = "0.1.4" # A (mostly) lockless slotmap implementation in Rust
pi_slotmap = "0.2.1" # Slotmap data structure
slotmap-fork-otter = "1.0.2" # Slotmap data structure - temporary fork for Otter
slotmap-slice-fork = "1.0.7" # Fork of slotmap
slotmap-careful = "0.7.0" # Wrap the slotmap crate and prevent key reuse
slotmap-map = "1.0.7" # Slotmap data structure
concurrent-slotmap = "0.2.0-alpha.1" # A lock-free concurrent slotmap
dense-slotmap-mem = "0.1.1" # Fixed-capacity, generation-tracked dense slot map with stable handles and raw memory in…
... and 73 crates more (use --limit N to see more)
note: to learn more about a package, run `cargo info <name>`
Okay so we have a way to search there is some standards for the dependency names:
• The first digit is the “major” version. Once released, crates try not to break
compatibility without increasing the major version number. Version zero
is special. Crates with a major version of 0 are pre-release—and are allowed
to break things.
• The second digit is the “minor” version. Changes that add functionality
but don’t break compatibility generally increment the minor version
number.
• The third digit is the “patch” version. A quick fix to a bug generally
increments the “patch” version
You can add a few qualifiers to provide fine-grained control over what crate version you use:
• =0.8.0 will only use version 0.8.0 - nothing lower or higher.
• ^0.8.0 will use any version equal to or greater than 0.8.0, but only within
the 0.x version range.
• ~0.8.0 allows patch updates like 0.8.1, but not 0.9.0 Updates will automatically be applied, even if they break the crate’s API.
Lastly you could even describe all the dependencies that you want to have in your project in the Cargo.toml under the [dependencies] section.
bracket-lib = { git = "https://github.com/thebracket/bracket-lib" }
# or
bracket-lib = {
git = "https://github.com/thebracket/bracket-lib",
default-features = false,
features = [ "amethyst_engine_vulkan" ]
}
If you want to get rid of a dependency you can run cargo clean to remove all the dependencies and then run the command to get back the dependencies cargo build
Wrap-Up
We have gotten started with Rust no we can start to take our first steps.