Introduction
Rust is a multi-paradigm, general-purpose programming language emphasizing performance, type safety, and concurrency. It is a language for systems programming developed by the Mozilla Foundation. The Rust language makes you a simple promise: if your program passes the compiler's checks, it is free of undefined behavior. It consists of 3 main components:
- cargo is Rust's compilation manager, package manager and general-purpose tool. You can use Cargo to start a new project, build and run your program, and manage any external libraries your code depends on.
- rustc is the Rust compiler. Usually we let Cargo invoke the compiler for us, but sometimes it's useful to run it directly.
- rustdoc is the Rust documentation tool. If you write documentation in comments of the appropriate form in your program's source code, rustdoc can build nicely formatted HTML from them. Like rustc, we usually let Cargo run rustdoc for us.
Installation
Official installation way:
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
But I got error, maybe because of KDE Neon:
info: downloading installer
curl: (6) Could not resolve host: static.rust-lang.org
rustup: command failed: downloader https://static.rust-lang.org/rustup/dist/x86_64-unknown-linux-gnu/rustup-init /tmp/tmp.ZijlkwgHu3/rustup-init x86_64-unknown-linux-gnu
So I used Ubuntu packages to install Rust by running:
sudo apt update
sudo apt install rustc cargo
To check that Rust got installed:
rustc --version
OUTPUT
rustc 1.43.0
You can also install additional development tools like Rust Analyzer and rust-src:
mkdir -p ~/.local/bin
curl -L https://github.com/rust-analyzer/rust-analyzer/releases/latest/download/rust-analyzer-x86_64-unknown-linux-gnu.gz | gunzip -c - > ~/.local/bin/rust-analyzer
chmod +x ~/.local/bin/rust-analyzer
sudo apt install rust-src
Hello World in Rust
Let's first create a simple hello world program:
mkdir hello_world
cd hello_world
And create a file called main.rs with the following content:
fn main() {
println!("Hello, world!");
}
And to compile and test run:
rustc hello_world.rs
./hello_world
Hello, world!
Cargo
To create a new project:
cargo new superproject
To build and run:
cargo build
cargo run
To clean generated files:
cargo clean