Day 3 - First Steps

In this article, I discuss how to install Rust and Cargo on macOS, followed by covering some basic concepts of a Rust project, such as its structure and how to build and run a simple "Hello, World!" program. The project setup involves using the `cargo new` command, creating a basic directory structure, and writing a simple `main.rs` file to output "Hello, world!" to the console. Various methods to build and run the program are explored, highlighting `rustc`, `cargo run`, and using an IDE.
In day 2 I forgot to mention how I installed Rust and Cargo since I had them installed some time ago, actually the installation is quite simple just go to the official installation page(https://www.rust-lang.org/tools/install) and follow the instructions. In my case it detected that I was using macOS and it was only necessary to execute the command:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Now back to the plan. On day 3 I will focus on some basic concepts:
Structure of a Rust project
How to build and run
Creating a “Hello, world!”
So, I’ll create the mythic “Hello World” project using the cargo command that is available when Rust is installed:
cargo new hello_world
If everything is ok, an output like this should be shown:
Creating binary (application) `hello_world` package
note: see more `Cargo.toml` keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Also a folder with the name of the project will be created, I’ll open that folder on RustRover.
Project Structure
This is the basic structure that is applicable to a wide project types:
my_rust_project/
├── Cargo.toml # This is the core configuration
├── src/ # Main source code
│ ├── main.rs # Entry point for binary projects
│ ├── lib.rs # Entry point for library projects
│ ├─ ... # Other files/folders to organize modules
├── tests/ # Integration tests
├── benches/ # Benchmarks
├── target/ # Compiler output
└── .gitignore # Git ignore file
Running our “Hello, world”
As this hello_world project is a binary, the code must be on main.rs and is only a function called main with a single statement to print “Hello, world!” on the console.
fn main() {
println!("Hello, world!");
}
There are few ways to build and run this program:
rustc
rustc main.rs #This will genereate a binary ./main
cargo
cargo run main.rs
IDE
After running the program, an output like this should be shown:
It may seem basic, but understanding this foundation is key. Next up: making Rust a bit more dynamic. See you on day 4!
Subscribe to my newsletter
Read articles from Juan Horta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
