HomeWho We AreContact

Beginning To Rust

By Neil Ludlow
Published in Software
September 12, 2024
1 min read
Beginning To Rust

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…

Rust Rover

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.gz
sudo mv jetbrains-toolbox-1.xx.xxxx jetbrains
jetbrains/jetbrains-toolbox # To open toolbox
sudo 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

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 Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
/home/neil/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
/home/neil/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
/home/neil/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
/home/neil/.profile
/home/neil/.bashrc
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
default host triple: x86_64-unknown-linux-gnu
default toolchain: stable (default)
profile: default
modify PATH variable: yes
1) Proceed with standard installation (default - just press enter)
2) Customize installation
3) 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 include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, you need to source
the 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/pdksh
source "$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)

Cargo

Creating a new project with cargo creates a Cargo.toml file and a “src” directory.

Creating a New Package using cargo

cargo --version
cargo new hello_world --bin
# --bin is not needed, it makes a binary target of src/main.rs which is the default
cd hello_world
cargo build
cargo run # build then run in one command
cargo build --release # build a release version
cargo check # checks code to see if it'll work but does not create executable
cargo update # update the versions of dependencies ignoring the Cargo.lock file

Find new dependencies here… Registry for Rust crates: crates.io

Add a new dependency Method 1

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 a new dependency Method 2

Add through cargo…

cargo add rand

Dependency gets added to “Cargo.toml”.

Clippy

Install new component, Clippy, which is a linter…

rustup component add clippy

Run /clippy with this command…

cargo clippy

Clippy docs

Run an executable

After running cargo build you should have an executable, in which case you can run the executable like this…

./target/debug/hello_world

Mutability

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;

See, Variables and Mutability

Printing Values

let x = 5;
let y = 10;
println!("x = {x} and y + 2 = {}", y + 2);

Conclusion

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


Tags

#rust#linux#cargo#linux

Share

Previous Article
Choices
Neil Ludlow

Neil Ludlow

Freelance Software Developer

Table Of Contents

1
Rust Rover
2
Install Rust
3
Cargo
4
Clippy
5
Run an executable
6
Mutability
7
Printing Values
8
Conclusion

Related Posts

Using Linux Find
August 25, 2024
3 min
© 2024, All Rights Reserved.
Powered By

Quick Links

About ShortdarkContact

Legal Stuff

Social Media