Many companies around the world are using Rust in production today for fast, low-resource, cross-platform solutions. Software you know and love, like Firefox, Dropbox, and Cloudflare, uses Rust. Rust is known for its ability to help developers write fast, reliable, and secure software.
Rust playground
Rust Playground is an online platform that allows developers to write, compile, and share Rust code directly in their browser, making it easy to experiment with the language without needing to install anything.
Getting Started
Installation
Open a terminal and enter the following command:
curl --proto '=https' --tlsv1.2 https://sh.rustup.rs -sSf | sh
The command downloads a script and starts the installation of the rustup tool, which installs the latest stable version of Rust.
Verify installation
To check whether you have Rust installed correctly, open the Terminal and run the following commands:
rustc --version
echo $PATH
Update Rust
To update Rust, open the Terminal and run the following command:
rustup update
Uninstall Rust
To uninstall Rust and rustup, open the Terminal and run the following command:
rustup self uninstall
Local Documentation
The installation of Rust includes a local copy of the documentation. To access the offline documentation, open the Terminal and run the following command:
rustup doc
Using rustc
rustc
is the Rust compiler. It is the tool used to compile Rust source code into executable binaries. When you write a Rust program, you use rustc to turn your code (written in .rs files) into a machine-readable format that can be executed by the operating system.
First program
Rust files use the .rs
extension.
Create main.ts
file and enter the following code.
fn main() {
println!("Hello, world!");
}
Save the file. Run rustc
to compile and run the main.ts
file. Open the terminal and enter the following command.
rustc main.rs
Run the compiled program. Open the terminal and enter the following command.
./main
rustc
is fine for simple programs, but as your project grows, you’ll want to manage all the options and make it easy to share your code.
Using Cargo
Cargo is Rust’s build system and package manager. Developers use Cargo for building projects, as it integrates rustc with additional features like dependency management and testing.
Check whether Cargo is installed by entering the following command in the terminal.
cargo --version
Create a Rust project
Open the Terminal and run the following command:
cargo new hello_world
Cargo creates files and directories in the project directory. It has also initialized a new Git repository along with a .gitignore file.
The structure of the project directory:
rust_project/
├── .gitignore
├── Cargo.toml
└── src/
└── main.rs
Cargo.toml
is the main configuration file for the project, which includes metadata like the project’s name, version, dependencies, and more.src/
is a source directory containing a default main.rs
file, which is the entry point for the application (for a binary project).
Navigate to the project directory by running the following command:
cd hello_world
Run the project
Open the Terminal and run the following command:
cargo run
Build project
Open the Terminal and run the following command:
cargo build
cargo build
generates an executable file (target/debug/hello_world
).
Run the built version
Open the Terminal and run the executable file using the following command:
./target/debug/hello_world
Update the project
You can update the code (main function). Open main.rs
file and type:
fn main() {
let text = "world";
println!("Hello, {}!", text);
}
Save the file and run the project.
cargo run