Week 10: 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 features.cargo new project_name
: creates a new “hello, world!” project.cargo build
: compiles your code.cargo run
: compiles and then runs your application.cargo new hello_rust
, it creates a simple project structure for you.src/main.rs
.fn main()
block is the entry point for every rust executable.println!
is a macro (not a function, note the !
) that prints text to the console.let
keyword.let mut
keyword.i32
, i64
) or unsigned (u8
, u32
). The number is the bits of space it takes up. i32
is the default.f32
(single-precision) and f64
(double-precision). f64
is the default.bool
with values true
or false
.char
, for a single unicode character, denoted with single quotes.fn
keyword.->
.return
keyword if it’s at the end.pytest
library to run our tests.cargo test
.#[test]
attribute.tests
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 test
.assert!(expression)
: Panics if the expression evaluates to false
. Useful for boolean checks.assert_eq!(left, right)
: Panics if the left and right arguments are not equal. This is the one you will use most often.assert_ne!(left, right)
: The opposite of assert_eq!
. Panics if the arguments are equal.src/main.rs
.cargo test
in your terminal to check your work locally.labs/lab10/README.md
.IS4010: App Development with AI