Restarting my Rust journey. I’ve started learning Rust more than once before.
Here’s my one more attempt at reading The Rust Programming Language from cover to cover and writing short notes after every chapter. Nothing fancy, just the things that I think Future Me will appreciate when I inevitably forget them six months from now.
So here’s Day 1.
Chapter 1 — Getting Started
Hello, World! in Rust.
fn main() {
println!("Hello, world!");
}Brief notes:
- Rust source files end with the `.rs` extension.
- `fn` is used to define a function.
- Every Rust program starts execution from the `main()` function.
- `println!` is not a function; it’s a macro, which is why it ends with an exclamation mark (`!`). `!` distinguishes macros from regular functions.
Cargo
Cargo is Rust’s build system and package manager. To create a new project:
bash cargo new hello_cargoCargo automatically creates the project structure along with a file called `Cargo.toml`. The `.toml` file contains information about the project, such as its name, version, and dependencies.
[package]
name = "hello_world"
version = "0.1.0"
edition = "2024"
[dependencies]The `[dependencies]` section is where we list external libraries our project uses. In the Rust ecosystem, these libraries are called crates.
Commands worth remembering
`cargo build`: Compiles the project and creates an executable inside the `target/debug` directory. The first time it is run, Cargo also creates a `Cargo.lock` file that records the exact versions of your project’s dependencies.
`cargo run`: Compiles and immediately runs the program.
`cargo check`: Instead of creating an executable, it simply checks whether the code compiles. It skips the final build step, hence it is faster than `cargo build`.
That’s it for Day 1. The goal isn’t to rush through the book. The goal is to actually remember it this time. See you in Day 2.
0 comments:
Post a Comment