Result and Option in short

🔔 Prelude: When writing Rust programs, Result and Option are data structures we can hardly avoid. I believe that rather than understanding their underlying mechanisms, we should first quickly figure out how to handle them. Gift Wrapping Actually, we don’t need to think of Option<T> and Result<T,E> as too complicated: Option contains the data we want, where the data type is T. However, there might be no data inside (None). But regardless, as long as what we care about is wrapped, it will be an Option. Result also contains the data we want, but it comes with another “restless” additional item E (Error). In fact, combinations of Result and Option are very common, such as Result<Option<T>, E>. Result provides a powerful and unified specification for Rust’s error handling. So, how do we open the gift box… The moment of unwrapping gifts is always exciting. ...

2026-09-10

Rust Notes 4

Range Rather than using C style loop, in Rust, we use ..= to indicate a range: for i in 1..=5 { println!("{}",i); } Output: 1 2 3 4 5 Sequences are only allowed for numeric or character types because they can be consecutive. for i in 'a'..='z' { println!("{}",i); } Char Use '' . "" is for the string fn main() { let c = 'z'; let z = 'ℤ'; let cat= '😻'; } Accept Unicode, occupy 4 bytes. ...

2026-09-10

Rust Notes 3

🔔 Prelude: In the end, all data types in Rust come from these primitive types. Basic types Every value in Rust has a specific data type In general, these can be divided into two categories: scalar types and compound types Scalar types include: Numbers: Signed integers (i8, i16, i32, i64, isize), unsigned integers (u8, u16, u32, u64, usize), floating-point numbers (f32, f64) The isize and usize types depend on the architecture of the computer the program is running on: if the CPU is 32-bit, these types are 32-bit; similarly, if the CPU is 64-bit, they are Rust’s default integer type is i32 Can overflow, and Rust does not check for this by default. If you encounter incorrect values, make sure to check the type of your numbers. Strings: String literals and string slices Booleans: true and false Characters: Representing a single Unicode character, stored as 4 bytes Unit type: Represented by (), with its only value also being () Be Careful with floats See this example: ...

2026-09-10

Rust Notes 2

Syntax of Rust Don’t worry, this will be very easy at the beginning. (Really?) Function goes first Similar to C and C++, there must be a main function as the entry point of the program. Use fn to declare a function, so concise. Use () for receiving parameters, and {} for the actual logic inside the function. Remember to use ; to end a statement. As I said, it’s similar to C and C++. Example: fn main() -> { println!("Hello, world!"); // "ln" means print with a new line after the content // What is "!"? Well, it means this function is actually a macro // For now, let's just use it as is // You don't need to learn everything to start using Rust. Heart. } A complete structure of function head in Rust should be: ...

2026-09-10

Rust Notes 1

About these notes These Rust notes are just a collection of my learning records. Since I’m a beginner in Rust, these notes are more like quick references or supplements, rather than a comprehensive Rust tutorial. No matter what, I hope you can still find parts that are useful to you. Why rust? Rust’s advantages include memory safety, high performance, and high productivity. To me, Rust appears to be one of the best implementations of C++. If you don’t get into deeper topics and only use prepared libraries, Rust’s syntax is simple and clear. Its built-in compiler cautions are also quite informative, which significantly decreases the amount of thinking required on developers. ...

2026-09-10