Rust | Part 2
Online IDE: Rust Playground (rust-lang.org)
Run: Build and run your code, and show the output. The Run option is the same as using the
cargo run
command.Build: Build your code, but don’t run the code. The Build option is the same as using the
cargo build
command.Test: Build your code, and run all the tests against the code. The Test option is the same as using the
cargo test
command.
The Rust playground — Training | Microsoft Learn
For best practices, read the Rust Cookbook: Table of Contents — Rust Cookbook (rust-lang-nursery.github.io)
Some key details:
- Cargo is Rust’s build tool and dependency manager
rustup
is the preferred way to install/update Rust- Rust libraries are updated every 6 weeks
- The
fn
keyword is used to declare a function - The
let
keyword declares variables
One question I had is what is the difference between using rustup
and brew install rust
? According to the Rust forum, “brew
will let you only have one version of Rust installed, whereas rustup
lets you have multiple [1]”.
Rust files end in .rs
. To compile the Rust file/source code use rustc <file-ending-in-.rs>
. This creates a file named after the Rust file but without a file extension, otherwise known as an executable/program. To run the resulting program, do ./<executable>
. In the below screenshot, we see the terminal/CLI commands to compile and execute the code, printing “Hello, world!”.
- Cargo can be used to create a Rust project
The command
cargo new hello-cargo
“generates a new directory named hello-cargo with a src subdirectory and adds two files:
hello-cargo/
Cargo.toml
src/
main.rs
The Cargo.toml file is the manifest file for Rust. It’s where you keep metadata for your project and also any dependencies.
The main.rs file in the src subdirectory is where you write your application code.
Now personally, I had the question of why there is an exclamation point behind the println!
function. The println!
function in Rust is not a function but a macro. Macros use !
to distinguish them from normal method calls. Rust will rewrite the code for you at compile time. For example, this code:
fn main () {
let x = 5;
println! ("{}", x);
}
will be converted to something like this at compile time:
#![feature(prelude_import)]
#[prelude_import]
use std::prelude::v1::*;
#[macro_use]
extern crate std;
fn main () {
let x = 5;
{
::std::io::_print(::std::fmt::Arguments::new_v1(
&["", "\n"],
&match (&x,) {
(arg0,) => [::std::fmt::ArgumentV1::new(arg0, ::std::fmt::Debug::fmt)],
},
));
};
}
We can see a bit of C with &x
to reference the memory of the x
variable. Also, use of the std
standard library.
The
println!
macro expects one or more input arguments, which it displays to the screen or standard output.
- Use
{}
in strings for value substitution
The
println!
macro replaces each instance of curly brackets{}
inside a text string with the value of the next argument in the list.
fn main() {
// Call println! with three arguments: a string, a value, a value
println!("The first letter of the English alphabet is {} and the last letter is {}.", 'A', 'Z');
}
- When a variable is declared, it can be “bound” to a value
In Rust, variable bindings are immutable by default. When a variable is immutable, after a value is bound to a name, you can’t change that value.
To mutate a value, we must first use the
mut
keyword to make a variable binding mutable.
// The `mut` keyword lets the variable be changed
let mut a_number = 10;
println!("The number is {}.", a_number);
// Change the value of an immutable variable
a_number = 15;
println!("Now the number is {}.", a_number);
Like the word ‘mutation’, ‘mutable’ has a similar prefix indicating change. This is an example of a changeable variable.
// The `mut` keyword lets the variable be changed
let mut a_number = 10;
println!("The number is {}.", a_number);
// Change the value of an immutable variable
a_number = 15;
println!("Now the number is {}.", a_number);
Next post on Rust will be about data types.
Sources:
And of course, I use italics by itself to represent answers received from ChatGPT/Bing Chat. The italics with the bold line on the side indicates sourcing from the Microsoft Course: The Rust playground — Training | Microsoft Learn.