I thought I’d have a quick look at Rust since it seems to be the hot language at the moment. The Rust docs look great and the initial setup was pretty easy.
From what I know so far, it seems like a similar idea to C but without the memory issues (malloc, etc), so easier than C. But while it may be easier than C, that’s not saying that much as I’d suggest C is pretty difficult to use for a beginner compared with higher level languages.
This is a very quick intro into Rust, mainly walking through the start of the ”Get started with Rust” tutorial, but I’ve added some other parts of the docs in with the intro stuff, and it’s for Linux…
I installed Jetbrains Toolbox on Ubuntu to have a look at which IDEs they had…
cd /opt/sudo tar -xvzf ~/Downloads/jetbrains-toolbox-1.xx.xxxx.tar.gzsudo mv jetbrains-toolbox-1.xx.xxxx jetbrainsjetbrains/jetbrains-toolbox # To open toolboxsudo apt install libfuse2 # If needed, to fix "can't open filetype" error
From Jetbrains toolbox, installed RustRover, which is currently free for educational use.
Install Rust…
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Welcome to Rust!This will download and install the official compiler for the Rustprogramming language, and its package manager, Cargo.Rustup metadata and toolchains will be installed into the Rustuphome directory, located at:/home/neil/.rustupThis can be modified with the RUSTUP_HOME environment variable.The Cargo home directory is located at:/home/neil/.cargoThis can be modified with the CARGO_HOME environment variable.The cargo, rustc, rustup and other commands will be added toCargo's bin directory, located at:/home/neil/.cargo/binThis path will then be added to your PATH environment variable bymodifying the profile files located at:/home/neil/.profile/home/neil/.bashrcYou can uninstall at any time with rustup self uninstall andthese changes will be reverted.Current installation options:default host triple: x86_64-unknown-linux-gnudefault toolchain: stable (default)profile: defaultmodify PATH variable: yes1) Proceed with standard installation (default - just press enter)2) Customize installation3) Cancel installation
Afterwards…
Rust is installed now. Great!To get started you may need to restart your current shell.This would reload your PATH environment variable to includeCargo's bin directory ($HOME/.cargo/bin).To configure your current shell, you need to sourcethe corresponding env file under $HOME/.cargo.This is usually done by running one of the following (note the leading DOT):. "$HOME/.cargo/env" # For sh/bash/zsh/ash/dash/pdkshsource "$HOME/.cargo/env.fish" # For fish
Get Rust version, therefore checking Rust is installed properly…
rustc --version
rustc 1.80.1 (3f5fd8dd4 2024-08-06)
Creating a new project with cargo creates a Cargo.toml file and a “src” directory.
Creating a New Package using cargo
cargo --versioncargo new hello_world --bin# --bin is not needed, it makes a binary target of src/main.rs which is the defaultcd hello_worldcargo buildcargo run # build then run in one commandcargo build --release # build a release versioncargo check # checks code to see if it'll work but does not create executablecargo update # update the versions of dependencies ignoring the Cargo.lock file
Find new dependencies here… Registry for Rust crates: crates.io
Add dependency to Cargo.toml
[package]name = "hello_world"version = "0.1.0"edition = "2021"[dependencies]time = "0.1.12"regex = "0.1.41"
The Cargo.toml file is the manifest.
Then, re-run cargo build
Add through cargo…
cargo add rand
Dependency gets added to “Cargo.toml”.
Install new component, Clippy, which is a linter…
rustup component add clippy
Run /clippy with this command…
cargo clippy
After running cargo build
you should have an executable, in which case you can run the executable like this…
./target/debug/hello_world
If a variable is immutable, it cannot be changed.
This would be a const
in javascript.
In Rust, variables are immutable, unless you specify they are mutable by adding mut
in front when creating the variable.
let mut x = 5;
let x = 5;let y = 10;println!("x = {x} and y + 2 = {}", y + 2);
This is a basic walkthrough of the start of the Rust tutorial, with some extra bits added, specifically around Cargo.
There may be a fast adoption of Rust, but as yet I do not have a reason to use it. It’s interesting to use it a little bit and see what all the fuss is about.
The official tutorial is here: Learning Rust by Writing a Command Line App in 15 Minutes
Quick Links
Legal Stuff