Summary
Rust (or rustlang) is one of modern programming languages for general purposes, which is fast, safe and productive for development.
Rust has a lot of features such as functional programming paradigm, ownership and zero cost abstractions (!). As to speed and safety, it doesn't have GC, garbage collection, so it runs with much smaller memory and cleanly. As to productivity, it has a nice package manager called cargo and also a toolchains (multiple release channels: stable, beta and nightly) installer called rustup.
It supports cross-platform so a single codebase can be compiled to be compatible with various platforms. Moreover, the output is a single binary and the size is optimized and therefore small, which provides another feature with Rust, portability.
The supported platforms are Windows, Mac, Linux (Tier 1). Not only them but also FreeBSD (Tier 2) and OpenBSD (Tier 3).
What are tiers ? They are about the platform support model of Rust. It consists of three layers.
Tier | Description |
---|---|
1 | "guaranteed to work" (with automated testing in building official binary) |
2 | "guaranteed to build" (with automated builds and partial automated testing) |
3 | may or may not work (with codebase support but without automated builds and testing) |
64-bit Linux (kernel 3.2+, glibc 2.17+) is Tier 1, the most familiar with !!
This post shows how to install Rust on Artix Linux based on Arch Linux.
Environment
- OS: Artix Linux (based on Arch Linux)
- App: Rust 1.66.0
- App toolchains installer: rustup 1.25.1
- Terminal: Xfce4 Terminal
- IDE (& Editor): VSCodium (or VS Code)
Tutorial
doas
at each can be replaced with sudo
.
Installation
You have at least two options even when you use pacman.
- Install Rust directly.
- Install rustup and get the environment of its stable channel.
The latter is officially recommended. Well, it's not essential. You can take the former as trial to take a shortcut.
Here are both examples. Open your terminal. Here we go.
Install Rust directly (Option 1)
$ doas pacman -Sy rust
Install rustup (Option 2)
$ doas pacman -Sy rustup
Then run:
$ rustup default stable
The output was:
info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2022-12-15, rust version 1.66.0 (69f9c33d7 2022-12-12)
info: downloading component 'cargo'
info: downloading component 'clippy'
info: downloading component 'rust-docs'
info: downloading component 'rust-std'
info: downloading component 'rustc'
68.0 MiB / 68.0 MiB (100 %) 22.9 MiB/s in 2s ETA: 0s
info: downloading component 'rustfmt'
info: installing component 'cargo'
info: installing component 'clippy'
info: installing component 'rust-docs'
19.0 MiB / 19.0 MiB (100 %) 15.3 MiB/s in 1s ETA: 0s
info: installing component 'rust-std'
29.7 MiB / 29.7 MiB (100 %) 17.4 MiB/s in 1s ETA: 0s
info: installing component 'rustc'
68.0 MiB / 68.0 MiB (100 %) 19.7 MiB/s in 3s ETA: 0s
info: installing component 'rustfmt'
info: default toolchain set to 'stable-x86_64-unknown-linux-gnu'
stable-x86_64-unknown-linux-gnu installed - rustc 1.66.0 (69f9c33d7 2022-12-12)
Done 😉
Besides, with rustup, you can take nightly or beta version instead of stable, which possibly has shorter compilation time or richer (but perhaps in testing) function.
Create a project
First of all, create a directory.
$ mkdir my-first-rust-prj
$ cd my-first-rust-prj
Next, start VSCodium (or VS Code) in the directory.
Then press Ctrl + Shift + @
with your keyboard to open terminal.
Run cargo init
to create a cargo package:
$ cargo init --bin .
Besides, --bin
option is the default and therefore may be omitted.
The output was:
Created binary (application) package
You will see Cargo.toml
, the project manifest file, Cargo.lock
, the packages lock file to its dependencies, and src/main.rs
, the program entry and so on. Additionally, target
directory will be created after cargo build
.
.
├─.gitignore
├─src
├───main.rs
├─Cargo.lock
└─Cargo.toml
Run a demo app
You got a set which is able to be compiled.
How to compile it and run as app ? Just run:
$ cargo run
The output was:
Compiling my-first-rust-prj v0.1.0 (/(...)/my-first-rust-prj)
Finished dev [unoptimized + debuginfo] target(s) in 0.26s
Running `target/debug/my-first-rust-prj`
Hello, world!
Yay. It said "Hello, world!" Brilliant :)
Alternatively, you may get the binary and execute it manually.
$ cargo build
Finished dev [unoptimized + debuginfo] target(s) in 0.00s
$ ./target/debug/my-first-rust-prj
Hello, world!
Conclusion
Now you have an example Rust project and are ready for development.
You can manipulate data, implement File I/O, handle network connection, build a server, etc.
Enjoy your journey(s) with Rust -- the safe, the powerful, the interesting, the robust and the charming.
Bon voyage ;)
References
-
The Rust Programming Language
- Really a great book !!
- Learn Rust