diff options
| author | user@node5.net <user@node5.net> | 2026-07-27 12:48:07 +0200 |
|---|---|---|
| committer | user@node5.net <user@node5.net> | 2026-07-27 12:48:07 +0200 |
| commit | 81595f425555450452128801fd3bf7776cfb1ba2 (patch) | |
| tree | 54dd4dd7ee192046347abf5e77da535b7a478b01 /misc/Learning rust | |
| parent | 154a705b2d978eb42b0c92875a8d564640dc2ce9 (diff) | |
learning rust chapter 3 up till temp assignment, model m research
Diffstat (limited to 'misc/Learning rust')
| -rw-r--r-- | misc/Learning rust/feel_the_validation.webp | bin | 0 -> 616 bytes | |||
| -rw-r--r-- | misc/Learning rust/index.md | 195 |
2 files changed, 195 insertions, 0 deletions
diff --git a/misc/Learning rust/feel_the_validation.webp b/misc/Learning rust/feel_the_validation.webp Binary files differnew file mode 100644 index 0000000..2af8a4f --- /dev/null +++ b/misc/Learning rust/feel_the_validation.webp diff --git a/misc/Learning rust/index.md b/misc/Learning rust/index.md new file mode 100644 index 0000000..dd8b064 --- /dev/null +++ b/misc/Learning rust/index.md @@ -0,0 +1,195 @@ +--- +description: Notes to self about learning rust +created: 2026-07-22 +--- + +The take aways from [__The__ book on rust](https://doc.rust-lang.org/stable/book/) that took my knowledge from point A to B as primarily a Python developer. + +## Chapter 2 + +### Declaring variales + +<https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#:~:text=let%20statement> + +`let` declares a variable in the local scope + +variables are immutable by default + +--- + +[*"The `::` syntax in the `::new` line indicates that new is an associated function of the String type"*](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#:~:text=The%20%3A%3A%20syntax%20in%20the%20%3A%3Anew%20line%20indicates%20that%20new%20is%20an%20associated%20function%20of%20the%20String%20type) + +--- + +in object oriented we call a function on an object a method + +--- + +In rust you can reference the same object, and do a object.clone, similar to python + +--- + +```rust +use std::io; + +fn main() { + println!("Guess the number!"); + + println!("Please input your guess."); + + let mut guess = String::new(); + + io::stdin() + .read_line(&mut guess) + .expect("Failed to read line"); + + println!("You guessed: {guess}"); +} +``` + +I'm learning rust, why do we instantiate a variable with `String::new();` only to append something to it afterwards, is it functional or for legibility, reply concisely. + +In Rust, `String::new()` is the common “start with an empty owned buffer” pattern. read_line(&mut guess) needs a String you can mutate (grow) as input arrives, so you create an empty one first. + +It’s functional (you need an owned, mutable String to pass to read_line), and also idiomatic/legible because it makes the intent—“this will hold the user’s input”—clear. + +--- + +[Result enum states are called variants](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#:~:text=Result%20is%20an%20enumeration%2C%20often%20called%20an%20enum%2C%20which%20is%20a%20type%20that%20can%20be%20in%20one%20of%20multiple%20possible%20states%2E%20We%20call%20each%20possible%20state%20a%20variant%2E) + +--- + +[Rust doesn’t yet include random number functionality in its standard library](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#:~:text=Rust%20doesn%E2%80%99t%20yet%20include%20random%20number%20functionality%20in%20its%20standard%20library) + +[The specifier 0.8.5 is actually shorthand for ^0.8.5, which means any version that is at least 0.8.5 but below 0.9.0.](https://doc.rust-lang.org/stable/book/ch02-00-guessing-game-tutorial.html#:~:text=The%20specifier%200%2E8%2E5%20is%20actually%20shorthand%20for%20%5E0%2E8%2E5%2C%20which%20means%20any%20version%20that%20is%20at%20least%200%2E8%2E5%20but%20below%200%2E9%2E0) + +## Chapter 3 + +### Looping chars + +```rust +fn main() { + for n in 1..u32::MAX { + let c: char = match char::from_u32(n) { + Some(j) => j, + None => { + // println!("Can't get number for char value {n}"); + continue; + } + }; + // print!(" {n} {c}"); + print!("{c}"); + } +} +``` + +Keeps trying with ints that don't have a unicode char + +> The rust book states: [*"Unicode scalar values range from U+0000 to U+D7FF and U+E000 to U+10FFFF inclusive"*](https://doc.rust-lang.org/stable/book/ch03-02-data-types.html#the-character-type:~:text=Rust%2E%20Unicode%20scalar%20values%20range%20from%20U%2B0000%20to%20U%2BD7FF%20and%20U%2BE000%20to%20U%2B10FFFF%20inclusive) + +Coming back to this after the end of chapter 3: + +```rust +fn main() { + for n in 1.. { + let c: char = char::from_u32(n).expect(&format!("Can't get number for char value {n}")); + print!("{c}"); + } +} +``` + +```console +thread 'main' (177732) panicked at src/main.rs:6:17: +Can't get number for char value 55296 +``` + +```rust +fn print(char: u32) { + let char: char = char::from_u32(char).expect(&format!("Can't get number for char value {char}")); + print!("{char}"); +} + +fn main() { + for n in 0..0xD7FF { + print(n); + } + for n in 0xE000..0x10FFFF { + print(n); + } + +} +``` + +Finally writing some rust i must admit that i find rusts compiler errors are really friendly, +the syntax is plesent and legible, and leads you to handle errors. + +### Temperature converter + +```rust +use std::io; + +fn celsius_to_fahrenheit(celsius: f64) -> f64 { + (celsius * 1.8) + 32.0 +} + +fn fahrenheit_to_celsius(fahrenheit: f64) -> f64 { + (fahrenheit - 32.0) / 1.8 +} + +fn main() { + println!("Input temperature in the format: <number><unit> e.g: 22C or 30.4F"); + + let mut input = String::new(); + io::stdin() + .read_line(&mut input) + .expect("Failed to read line"); + + let (temp, unit) = input.trim().split_at(input.len() - 2); + let unit = unit.to_uppercase(); + + let temp: f64 = temp.parse().expect(&format!("Couldn't parse number from: {temp}")); + println!("temp: {temp}, unit: {unit}"); + + let result_unit: char; + let result: f64 = match unit.as_str() { + "F" => { + result_unit = 'C'; + fahrenheit_to_celsius(temp) + } + "C" => { + result_unit = 'F'; + celsius_to_fahrenheit(temp) + } + _ => panic!("Unknown unit {unit}"), + }; + println!("{temp}°{unit} = {result}°{result_unit}") + +} +``` + +Asking an ai bot to give feedback + +``` +I'm learning rust, coming from python and some C programming, I've read the first 3 chapters of the rust book, which covers: +... +``` + +``` +Your code is solid Rust and it mostly avoids common beginner pitfalls from the book—but there are a few correctness/robustness issues and a couple “Rust-idiomatic” improvements. +``` + +It validates me, wow, what a supprise  + + +1. split_at(input.len() - 2) is brittle and can be incorrect because you compute the index from the untrimmed string length (so whitespace/newline can break the split). +2. Slicing by byte index is unsafe for general cases; indexing must land on a valid char boundary (your current approach assumes a fixed layout). +3. Parsing by “last 2 characters” assumes the unit is always exactly one character and located at a fixed position; inputs like 22 C, missing unit, etc., will fail or behave unexpectedly. +4. Your match works, but using result_unit + mutation in match arms is less idiomatic; returning both values directly is cleaner. +5. Using panic!/expect for invalid user input is fine for an exercise, but for real programs you’d prefer returning Result and printing a friendly error. + +If you want a robust fix: parse from let trimmed = input.trim(); and take the last non-whitespace character as the unit, then parse the remaining prefix as the number. + +--- + + +### Chapter 4 |
