Inspired by @ihssmaheel and his guide to Rust for JavaScript developers I decided to start learning this general-purpose programming language from scratch. Of course, I do know other languages, but JavaScript is the one I’m using every day at work for years. I’ll share my progress with you in this beginners-oriented series.
Before proceeding, here’s what I’m going to do: I plan to follow The Rust Programming Language, but in its forked version published by the Brown University. I chose to do so, because this one is more interactive than the original version, and I think it would be better for me. It’s also recommended by the language maintainers.
Installing Rust on macOS
I followed the official instructions about how-to install Rust on UNIX-like systems, so I just entered the following command, then I pressed [Enter]
without customizing anything. An interactive prompt is provided, if you want to change the installation path. Windows users and others have an alternate method to get started.
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
The command above should work in most cases—and you can execute rustc --version
to check if Rust is already on your path. Otherwise, you must fix your configuration accordingly. I’m assuming that you already have Xcode Command Line Tools installed: if you don’t, you can easily get it.
xcode-select --install
Don’t blame me, I used the #beginners topic on purpose. That said, you should be able to compile further dependencies with no issues. Linux users can rely on GCC, while Windows users will get it along with Rust itself—and either use cmd.exe
or PowerShell as terminal emulator.
Integrating with VS Code
This is optional, but I’ve also installed rust-analyzer to support Rust on VS Code. For sure there are different extensions available to date, but this one is maintained from the language developers directly: it has been updated recently, so I think it’s worth it. You can find alternative IDE integrations available.
Hello, World!
I will follow the instructions for UNIX-like systems, but you can use PowerShell to replicate the same steps without changes on Windows. The book (you can even get a paperback or e-book copy) suggests to create a dedicated folder for the projects you’re going to build, then one for your first Hello, World!
program.
mkdir ~/projects
cd ~/projects/
mkdir hello_world
cd hello_world/
I didn’t choose to place the projects
folder in my user’s home, and you can create it wherever you want, but I suggest to continue on VS Code or your IDE of choice, opening a terminal emulator below the project sources. Create a new main.rs
file under the hello_world
directory and paste the following code.
fn main() {
println!("Hello, world!");
}
The function structure doesn’t differ a lot from JavaScript, but Rust uses a 4-spaces indentation like Python—I prefer 2-spaces where possible, then I must keep in mind this rule. You can now compile the main.rs
file, which will output a main
executable: it will just print Hello, world!
in the terminal emulator.
rustc main.rs
./main
If you noticed, rust-analyzer returned an error. I didn’t use Cargo already, so it’s normal that the extension can’t find a dedicated workspace. You should get an ERROR FetchWorkspaceError: rust-analyzer failed to fetch workspace
in VS Code OUTPUT tab. I’ll fix it in the next journeys.
Cargo Package Manager
Rust comes with its own package manager called Cargo, and an online registry of packages you can browse. Here things get more complicated, since it provides a set of commands to help creating, running, and building programs: just like npm
, yarn
, etc. do for JavaScript.
cd ..
cargo new hello_cargo
cd hello_cargo/
By default, Cargo initializes a new empty Git repository, but you can override this behavior inspecting its optional arguments. Let’s have a look at the folder structure: nothing new, except for the file names. This sample does exactly the same of the previous exercise, and shares the same source code.
hello_cargo
├── Cargo.toml
└── src
└── main.rs
Cargo.toml
can be seen as an equivalent of package.json
. It includes a list of properties formatted in TOML, a minimal configuration language I’ve already seen on AWS to set different profiles for the same organization account, for example. This file lists details about the package, as well as optional dependencies.
[package]
name = "hello_cargo"
version = "0.1.0"
edition = "2024"
[dependencies]
The Cargo CLI allows to define additional fields, but I guess I’ll understand more in the next chapters. Talking about the development cycle, Rust is very close to JavaScript, since you can check
, build
, or even run
a package: here’s the full list of steps to get things done.
Checking
Entering cargo check
will just check the package for errors, without building a binary nor executing it. In addition, you’ll see that the command creates a new folder called target
under src
, which now includes only the debug. This is valid for the other two steps, if it doesn’t exist yet, but it will get the binary as well.
Building
On the other hand, cargo build
will actually produce a binary, ready to be executed: it’s not optimized by default, since you must append --release
if you really want to go into production. I think that you’d find it familiar, coming from different programming languages—apart from the appended option which can vary.
Running
The cargo run
command will compile the sources like the previous one, but it executes the produced binary as well: then, you’ll see Hello, world!
printed right in the terminal along with the debug information. This is the fastest way to build and run a package at the same time, but it doesn’t produce optimized code.
That’s all for today. I better understand how Rust works already, but it will be a long way though: the book gives a list of more detailed information about Cargo, but the package manager has its own documentation and I think I’ll read even more about it in the next few days. Let me just say that Ferris is a pretty cool mascotte!
Top comments (1)
Didn’t expect to see my name here. Thanks for the mention !! Excited to follow your Rust journey.