Week 09: Welcome to Rust
cargo, your Rust build toolcargo is Rust’s official build system and package manager, and it’s one of the language’s best featuresProject management: - cargo new project_name: creates a new “Hello, world!” project - cargo init: initialize Cargo in an existing directory
Building and running: - cargo build: compiles your code (debug mode) - cargo run: compiles and runs in one command - cargo build --release: optimized production build
Development workflow: - cargo check: fast compile check (no executable) - cargo test: run all tests - cargo clean: delete build artifacts
cargo clippy - your personal Rust mentor: - Catches common mistakes and suggests better approaches - Over 500 lints for code quality, performance, and style - Provides automated feedback based on established Rust lints - Example: “you can simplify this code” or “this might panic”
cargo fmt - automatic code formatting: - Formats your code to match Rust community style - No more debates about spacing, indentation, or style - Run before every commit - keeps code consistent - Like Python’s black formatter
Use this repeatable development loop:
cargo check - fast compilation check while codingcargo clippy - catch issues and learn better patternscargo test - verify your code works correctlycargo fmt - format code before committingcargo run - try out your programPractice: Use cargo check for quick compiler feedback, then run the full required checks before submitting.
Open: play.rust-lang.org
Follow along:
Challenge: Can you make it print TWO lines with different messages?
cargo new hello_rust, it creates a simple project structure for yousrc/main.rsfn main() function is the entry point for a standard Rust binaryprintln! is a macro (not a function, note the !) that prints text to the consoleTry it yourself: ▶ Run in Rust Playground
Your turn: Modify the message and add a second println! line!
let keywordlet mut keywordTry it yourself: ▶ Experiment in Playground
Challenge: Uncomment the x = 6; line. What error do you get? Read it carefully!
i32, i64) or unsigned (u8, u32). The number is the bits of space it takes up. i32 is the defaultf32 (single-precision) and f64 (double-precision). f64 is the defaultbool with values true or falsechar, for a single Unicode character, denoted with single quotes&str and String"Hello", is a string literal. Its type is &str: fixed text built into your programString is text your program owns and can grow, created with String::from("Homer")char ('H'); double quotes make a &str ("H")& means “a reference.” Week 10 explains references fully; for now, read &str as “some text you can look at”Try it yourself: ▶ Run in Playground
Challenge: Remove mut from name. What does the compiler say?
Try it yourself: ▶ Run in Playground
Challenge: Try accessing my_tuple.0 and my_array[0]. What do you get?
fn keyword->return keyword if it’s at the endTry it yourself: ▶ Run in Playground
Challenge: Add a semicolon after num1 + num2. What error appears? Why?
pytest library to run our testscargo test#[test] attributetests inside the same file as the code they are testing#[cfg(test)] attribute tells Rust to only compile and run this code when we execute cargo testassert!(expression): Panics if the expression evaluates to false. Useful for boolean checksassert_eq!(left, right): Panics if the left and right arguments are not equal. This is the one you’ll use most oftenassert_ne!(left, right): The opposite of assert_eq!. Panics if the arguments are equaltodo!() in src/main.rscargo test, cargo fmt, and cargo clippy -- -D warningsweek09/, push, and inspect the Week 09 badgeweek09 Cargo projectsrc/main.rsweek09/lab09.mdIS 4010: App Dev with AI