Getting Started with Rust
2026-08-25
Systems programming is resource-constrained programming
Is this the inevitable cost of low-level control? No!
For any reasonable language, we either have a choice
Safe Rust: restrict to programs it can prove correct.
(There is also unsafe Rust, which we will mostly avoid.)
For heap objects (and the like):
This is all checked by a borrow checker.
Challenge: A performant doubly-linked list in Rust is hard!
Key reference: https://doc.rust-lang.org/book/
Rough plan is
Do read and practice outside lecture! And join us on Ed!
At the shell, run cargo new hello for new Rust project
hello/
Cargo.toml
src/
main.rs
Within hello, run cargo run
Compiling hello v0.1.0 (/Users/dbindel/work/class/cs4414-f26/hello)
Finished `dev` profile [unoptimized + debuginfo] target(s) in 3.60s
Running `target/debug/hello`
Hello, world!
We can also try this out in the playground.
What is in src/main.rs?
fn introduces a keyword or functionprintln! is a macro (vs a function)let name: type = value defines an immutable variableanswer is interpolated into the format string| Types | Literals | |
|---|---|---|
| Signed | i8, i16, i32, i64, i128, isize |
-1, 0, 1_000, 123_i64 |
| Unsigned | u8, u16, u32, u64, u128, usize |
0, 123, 10_u16 |
| Floating point | f32, f64 |
3.14, -6.02e23, 2_f32 |
| Unicode | char |
'a', '🦀' |
| Boolean | true, false |
&[T] type is a slice
&mut [T](ptr, len)Vec type (a struct) for variable len arrays (on heap)structs and enums laters[0])b"foo" for u8 literal arraysString type (a struct)Rust has similar arithmetic (and logic) operations to C.
&& and ||But consider:
Error turns into a runtime panic if we move the multiplication into a function.
strict_mul: Panic on overflowsaturating_mul: Largest integer on overflowwrapping_mul: Two’s complement wrap on overflowa * b varies with compile mode
strict_mul in debug modewrapping_mul in release modeadd and other operationsWhat is the output in release mode?
Hint: \(10000 = 8192 + 1024 + 512 + 256 + 16\)
We saw something like this already:
fn name(args) -> return_typename: type pairsreturn or through value of blockWhat happens with this code? Let’s try!
41 was never usedif syntax like C, but without parens on conditioncond ? a : b)bool (no automatic conversion)loop introduces an otherwise-infinite loopbreak and continue similar to Cbreak can produce a value for the loopabs is a method call (more on this later)while syntax like C, but without parens on conditionfor iterates over an iteratorlo..hi for range lo <= i < hilo..=hi for range lo <= i <= hiMany things about Rust “rhyme with” C/C++
We start seeing the big differences on Thursday
Structs and (especially) Enums