Alright, we've covered the basics of Rust. Now it's time to roll up our sleeves and get coding!
There are a few ways to install Rust, but we'll use the recommended method: a tool called rustup
. Think of rustup
like NVM (Node Version Manager) for Node.js – it helps manage Rust installations. Head over to the official Rust installation page (Installation Link), and it'll automatically detect your operating system and guide you through setting up rustup
.
Once you've got rustup
installed, you should have access to three important tools. You can check if they're up and running (and see their versions) using these commands in your terminal:
$ cargo --version cargo 1.83.0 (5ffbef321 2024-10-29) $ rustc --version rustc 1.83.0 (90b35a623 2024-11-26) $ rustdoc --version rustdoc 1.83.0 (90b35a623 2024-11-26)
- Cargo: This is your Swiss Army knife for Rust projects. It compiles your code, installs packages, and handles other general tasks.
- rustc: The Rust compiler itself. We usually compile code through cargo, but having rustc directly can be handy sometimes.
- rustdoc: This tool builds and runs Rust's official documentation offline, so you can easily browse references without needing an internet connection.
Now, let's create a brand new project! In your terminal, type this command:
$ cargo new hello
This creates a directory named hello
that holds your project files. Inside hello
, you'll find a directory named src
and a file called Cargo.toml
. The src
directory is where your source code goes. Inside src
, you'll find a file named main.rs
that has some boilerplate code for a classic "Hello, world!" program.
Ready to say hello to the world? Run this command in your terminal:
$ cargo run
If everything went smoothly, you should see the following printed to your screen:
Hello, world!
Behind the Scenes: Cargo.toml
The "Cargo.toml" file stores your project's configuration. Here's what you might find inside:
name = "hello" version = "0.1.0" edition = "2021" [dependencies]
-
name
: The obvious one, this is the name of your project. -
version
: This tells us the version of your application. Rust releases new editions every three years, which may involve breaking changes. The latest edition is currently 2021, with 2024 on the horizon. -
edition
: This specifies the Rust edition your project is using. -
[dependencies]
: This is where you'll list any external libraries your project relies on (it's empty since we haven't used any yet).
Now that we're all set up, it's time to dive headfirst into Rust programming! Stay tuned for the next part!