From d154b1e376a0f8b6a2aab8b053034261d47acafe Mon Sep 17 00:00:00 2001 From: Bartal Laearsson Date: Tue, 6 Oct 2026 20:58:16 +0100 Subject: [PATCH] oh just cargo fmt it all --- .../src/main.rs | 5 +- .../src/main.rs | 66 +++++++++---------- .../src/main.rs | 26 +++++--- .../src/control_flow.rs | 5 +- common-programming-concepts/src/data_types.rs | 34 +++++++--- common-programming-concepts/src/functions.rs | 4 +- common-programming-concepts/src/main.rs | 4 +- enums/src/defining_enums.rs | 6 +- enums/src/match_control_flow.rs | 2 +- guessing-game/src/main.rs | 2 +- ownership/src/ownership.rs | 18 +++-- ownership/src/references_and_borrowing.rs | 6 +- ownership/src/slice_type.rs | 1 - structs/src/defining_structs.rs | 38 +++++------ structs/src/main.rs | 2 +- structs/src/methods.rs | 5 +- structs/src/struct_program.rs | 11 ++-- 17 files changed, 119 insertions(+), 116 deletions(-) diff --git a/common-programming-concepts-fibonacci/src/main.rs b/common-programming-concepts-fibonacci/src/main.rs index 8414612..66c922f 100644 --- a/common-programming-concepts-fibonacci/src/main.rs +++ b/common-programming-concepts-fibonacci/src/main.rs @@ -5,7 +5,9 @@ fn main() { println!("Please insert a number between 2 and 45"); let mut num = String::new(); - io::stdin().read_line( &mut num).expect("failed to read line"); + io::stdin() + .read_line(&mut num) + .expect("failed to read line"); let num: u8 = match num.trim().parse() { Ok(n) if n < 45 => n, @@ -19,7 +21,6 @@ fn main() { println!("{fib}"); } - fn fibonacci(n: u8) -> u32 { if n == 0 { 0 diff --git a/common-programming-concepts-temp-converter/src/main.rs b/common-programming-concepts-temp-converter/src/main.rs index 4dddb4e..ba94ffa 100644 --- a/common-programming-concepts-temp-converter/src/main.rs +++ b/common-programming-concepts-temp-converter/src/main.rs @@ -2,7 +2,7 @@ use std::io; fn main() { println!("Welcome to the temperature converter program!"); - + // TODO USER INPUT 1: FAHRENHEIT OR CELCIUS // First we need to get an input from the user. In this case, I think it is best to like choose // a number, because what text should they write? f to c? or fc? or only f? that gets @@ -15,60 +15,56 @@ fn main() { // then the input should be sanitized and validated / parsed int. loop { + println!("Type 1 for converting Celcius to Fahrenheit"); + println!("Type 2 for converting Fahrenheit to Celcius"); - println!("Type 1 for converting Celcius to Fahrenheit"); - println!("Type 2 for converting Fahrenheit to Celcius"); + let mut temp_type = String::new(); + let mut degrees = String::new(); - let mut temp_type = String::new(); - let mut degrees = String::new(); + io::stdin() + .read_line(&mut temp_type) + .expect("failed to read line"); - io::stdin() - .read_line(&mut temp_type) - .expect("failed to read line"); - - let temp_type: u32 = match temp_type.trim().parse() { - Ok(num) if num == 1 || num == 2 => num, - Ok(_) => { + let temp_type: u32 = match temp_type.trim().parse() { + Ok(num) if num == 1 || num == 2 => num, + Ok(_) => { println!("Please type 1 or 2"); continue; } - Err(_) => { - println!("Please type 1 or 2"); - continue; - + Err(_) => { + println!("Please type 1 or 2"); + continue; } - }; + }; + // TODO USER INPUT 2 + // next up, we need to get a new user input, as float. This must also be sanitized and parsed. + // Here we can perhaps come into some difficulties, what if the user inputs 22? not 22.0? also, + // we need to let the user input again, if the input is invalid, like text or whatever. + println!("Please enter the temperature: "); - // TODO USER INPUT 2 - // next up, we need to get a new user input, as float. This must also be sanitized and parsed. - // Here we can perhaps come into some difficulties, what if the user inputs 22? not 22.0? also, - // we need to let the user input again, if the input is invalid, like text or whatever. - println!("Please enter the temperature: "); + io::stdin() + .read_line(&mut degrees) + .expect("failed to read line"); - io::stdin() - .read_line(&mut degrees) - .expect("failed to read line"); - - let degrees: f64 = match degrees.trim().parse() { + let degrees: f64 = match degrees.trim().parse() { Ok(num) => num, Err(_) => continue, }; - // TODO CALCULATOR - // The control flow starts here. if user input 1 = 1 then convert from c to f. if user input = - // 2, then f to c. We could have a else to catch something unexpected. Say something like, an - // error occured when trying to convert temperature, please try again... - if temp_type == 1 { - let result = (degrees * 9.0/5.0) + 32.0; + // TODO CALCULATOR + // The control flow starts here. if user input 1 = 1 then convert from c to f. if user input = + // 2, then f to c. We could have a else to catch something unexpected. Say something like, an + // error occured when trying to convert temperature, please try again... + if temp_type == 1 { + let result = (degrees * 9.0 / 5.0) + 32.0; println!("{degrees} celcius is {result:.2} fahrenheit"); } else if temp_type == 2 { - let result = (degrees - 32.0) * 5.0/9.0; + let result = (degrees - 32.0) * 5.0 / 9.0; println!("{degrees} fahrenheit is {result:.2} celsius"); } else { println!("something went wrong, please try again"); continue; } - } } diff --git a/common-programming-concepts-twelve-days-of-christmas/src/main.rs b/common-programming-concepts-twelve-days-of-christmas/src/main.rs index 4ca8cc0..8a6e024 100644 --- a/common-programming-concepts-twelve-days-of-christmas/src/main.rs +++ b/common-programming-concepts-twelve-days-of-christmas/src/main.rs @@ -1,13 +1,23 @@ fn main() { - let gifts: [&str;12] = ["A partridge in a pear tree", "Two turtle doves and", "Three french hens ", - "Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", - "Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", - "Eleven pipers piping", "Twelve drummers drumming" ]; + let gifts: [&str; 12] = [ + "A partridge in a pear tree", + "Two turtle doves and", + "Three french hens ", + "Four calling birds", + "Five golden rings", + "Six geese a-laying", + "Seven swans a-swimming", + "Eight maids a-milking", + "Nine ladies dancing", + "Ten lords a-leaping", + "Eleven pipers piping", + "Twelve drummers drumming", + ]; let mut s = String::from(gifts[0]); for day in 1..13 { - let ending = if day == 1 { + let ending = if day == 1 { "st" } else if day == 2 { "nd" @@ -16,10 +26,10 @@ fn main() { }; println!("\nOn the {day}{ending} day of Christmas, my true love sent to me"); println!("{s}"); - + if day != 12 { - s.insert_str(0,"\n"); - s.insert_str(0,gifts[day]); + s.insert_str(0, "\n"); + s.insert_str(0, gifts[day]); } } } diff --git a/common-programming-concepts/src/control_flow.rs b/common-programming-concepts/src/control_flow.rs index 0afea31..5cb84a9 100644 --- a/common-programming-concepts/src/control_flow.rs +++ b/common-programming-concepts/src/control_flow.rs @@ -30,7 +30,7 @@ pub fn run() { // You can also use if in a let statements, as such let condition = true; - let number = if condition {5} else {6}; + let number = if condition { 5 } else { 6 }; println!("The value of the conditional number is {number}"); // repitition with loops @@ -55,7 +55,7 @@ pub fn run() { // guess. Let's go slowly through this let mut count = 0; - 'counting_up: loop{ + 'counting_up: loop { println!("count = {count}"); let mut remaining = 10; @@ -99,5 +99,4 @@ pub fn run() { println!("{number_for}"); } println!("for loop LIFTOFF!!!"); - } diff --git a/common-programming-concepts/src/data_types.rs b/common-programming-concepts/src/data_types.rs index c72f827..5726d7f 100644 --- a/common-programming-concepts/src/data_types.rs +++ b/common-programming-concepts/src/data_types.rs @@ -7,7 +7,6 @@ pub fn run() { println!("delcared floats: \nvar x: {x} \nvar y: {y}"); - println!("\n=== 2.2 numeric ops ===\n"); // addition let sum = 5 + 10; @@ -24,9 +23,9 @@ pub fn run() { // remainder let remainder = 43 % 5; - println!("5 + 10: {sum}\n95.5 - 4.3: {difference}\n4 * 30: {product}\n56.7 divided by 32.2: {quotient}\ntruncated -5 / 3: {truncated}\n43 % 5: {remainder}\n"); - - + println!( + "5 + 10: {sum}\n95.5 - 4.3: {difference}\n4 * 30: {product}\n56.7 divided by 32.2: {quotient}\ntruncated -5 / 3: {truncated}\n43 % 5: {remainder}\n" + ); println!("\n=== 2.2 compound types ===\n"); println!("\n=== tuples ===\n"); @@ -55,15 +54,30 @@ pub fn run() { let index_one = tup2.1; let index_two = tup2.2; - println!("here are the tuple decustructed indexed values method: index zero: {}, index one: {}, index two: {}", index_zero, index_one, index_two); + println!( + "here are the tuple decustructed indexed values method: index zero: {}, index one: {}, index two: {}", + index_zero, index_one, index_two + ); println!("\n=== arrays ===\n"); // Arrays can only contain one type. Arrays in rust have a fixed length. // syntax is as such: let a = [1,2,3,4,5]; // arrays live on the stack. only use if you always know the number of items in the array. // Else, chances are you want to use vectors from the std lib. Vectors live in the heap. This is covered later in the book. - let months = ["January", "February", "March", "April", "May", "June", "July", - "August", "September", "October", "November", "December"]; + let months = [ + "January", + "February", + "March", + "April", + "May", + "June", + "July", + "August", + "September", + "October", + "November", + "December", + ]; // you can initialize the array with type and length as such: // let a: [i32;5] = [1,2,3,4,5]; @@ -73,9 +87,9 @@ pub fn run() { let third_month = months[2]; let fifth_month = months[4]; - println!("now we access the month array by index. The third month is {third_month}. The fifth month is {fifth_month}"); + println!( + "now we access the month array by index. The third month is {third_month}. The fifth month is {fifth_month}" + ); // Note, the compiler will complain if you try to access a value that is outside the array - - } diff --git a/common-programming-concepts/src/functions.rs b/common-programming-concepts/src/functions.rs index 5f0adcd..55f792a 100644 --- a/common-programming-concepts/src/functions.rs +++ b/common-programming-concepts/src/functions.rs @@ -29,8 +29,8 @@ fn print_labeled_measurement(value: u32, unit_label: char) { } // Statements and expressions -// Okay, as I understand it, statements are the majority of code we have written so far. -// statements end with semi colon (;), and do not return anything. +// Okay, as I understand it, statements are the majority of code we have written so far. +// statements end with semi colon (;), and do not return anything. // Expressions do NOT end with (;), but do return something. Look at these examples: fn five() -> u32 { diff --git a/common-programming-concepts/src/main.rs b/common-programming-concepts/src/main.rs index db500ee..cc44e34 100644 --- a/common-programming-concepts/src/main.rs +++ b/common-programming-concepts/src/main.rs @@ -1,7 +1,7 @@ -mod variables; +mod control_flow; mod data_types; mod functions; -mod control_flow; +mod variables; fn main() { variables::run(); diff --git a/enums/src/defining_enums.rs b/enums/src/defining_enums.rs index 33ca288..5d37e55 100644 --- a/enums/src/defining_enums.rs +++ b/enums/src/defining_enums.rs @@ -6,8 +6,6 @@ enum IpAddrKind { V6, } - - // One might try to use an enum and a struct to give it types, like this: #[derive(Debug)] struct IpAddr { @@ -22,7 +20,6 @@ enum IpAddrConcise { V6(String), } - pub fn run() { println!("\n=== defining enums ===\n"); route(IpAddrKind::V4); @@ -49,9 +46,8 @@ pub fn run() { println!("the home address is {home:#?}"); println!("the loopback address is {loopback:#?}"); - } -fn route (ip_kind: IpAddrKind) { +fn route(ip_kind: IpAddrKind) { println!("your ip kind is {ip_kind:#?}"); } diff --git a/enums/src/match_control_flow.rs b/enums/src/match_control_flow.rs index d41760f..6532f01 100644 --- a/enums/src/match_control_flow.rs +++ b/enums/src/match_control_flow.rs @@ -3,7 +3,7 @@ use crate::match_control_flow::UsState::Alabama; #[derive(Debug)] enum UsState { Alabama, - Alaska + Alaska, } // Her we define a basic enum diff --git a/guessing-game/src/main.rs b/guessing-game/src/main.rs index 1fbbf95..14fc8f9 100644 --- a/guessing-game/src/main.rs +++ b/guessing-game/src/main.rs @@ -53,7 +53,7 @@ fn main() { Ordering::Less => println!("Too small!"), Ordering::Greater => println!("Too big!"), Ordering::Equal => { - println!("You win!"); + println!("You win!"); break; } } diff --git a/ownership/src/ownership.rs b/ownership/src/ownership.rs index eef4785..e5f78f6 100644 --- a/ownership/src/ownership.rs +++ b/ownership/src/ownership.rs @@ -6,25 +6,23 @@ pub fn run() { // arrays and simple variables are allocated to the stack. These have fixed and known lentghs at // compile time. This is fast on the stack. // However, when working with data of unknown lengths, for example String::new(), Rust cannot - // know the length at compile time. This is then allocated to the heap with a reference in memory. - + // know the length at compile time. This is then allocated to the heap with a reference in memory. + // OWNERSHIP RULES // Each value in Rust has an owner // There can only be one owner at a time // When the owner goes out of scope, the value is dropped. - + // VARIABLE SCOPE - + // This is a string literal. This is hardcoded into the program at compile time. - - { // s is not valid here, since it has not been declared - let s = "hello"; // s is valid from this point onward + + { + // s is not valid here, since it has not been declared + let s = "hello"; // s is valid from this point onward println!("{s}"); // Do stuff with s - } // this scope is now over, and the variable s is no longer valid // when s comes into scope, it is valid // when s goes out of scope, it is invalid - - } diff --git a/ownership/src/references_and_borrowing.rs b/ownership/src/references_and_borrowing.rs index c6e202d..aec781f 100644 --- a/ownership/src/references_and_borrowing.rs +++ b/ownership/src/references_and_borrowing.rs @@ -9,7 +9,6 @@ pub fn run() { // Here is how you can use a variable as a reference to a parameter without taking ownership of // it - // s is a reference to s1 which has a reference to an address in memory let s1 = String::from("hello"); let len = calculate_length(&s1); @@ -20,10 +19,10 @@ pub fn run() { // At any given time, you can have either one mutable reference or any number of immutable references. // References must always be valid. - } -fn calculate_length(s: &String) -> usize { // s is a reference to a string +fn calculate_length(s: &String) -> usize { + // s is a reference to a string s.len() } // Here, s goes out of scope. But because s does not have ownership of what it refers to // The String is not dropped. @@ -49,5 +48,4 @@ fn calculate_length(s: &String) -> usize { // s is a reference to a string fn change(some_string: &mut String) { some_string.push_str(", world!"); - } diff --git a/ownership/src/slice_type.rs b/ownership/src/slice_type.rs index b2e7871..727d672 100644 --- a/ownership/src/slice_type.rs +++ b/ownership/src/slice_type.rs @@ -14,7 +14,6 @@ pub fn run() { let f_word = first_word(&s); println!("{f_word}"); - let my_string = String::from("hello world"); // `first_word` works on slices of `String`s, whether partial or whole. diff --git a/structs/src/defining_structs.rs b/structs/src/defining_structs.rs index e563df9..b162d71 100644 --- a/structs/src/defining_structs.rs +++ b/structs/src/defining_structs.rs @@ -1,14 +1,14 @@ #![allow(dead_code)] #![allow(unused_variables)] - struct Color(i32, i32, i32); - struct Point(i32, i32, i32); +struct Color(i32, i32, i32); +struct Point(i32, i32, i32); - struct User { - active: bool, - username: String, - email: String, - sign_in_count: u64, - } +struct User { + active: bool, + username: String, + email: String, + sign_in_count: u64, +} pub fn run() { println!("\n=== defining and instantiating structs ===\n"); @@ -21,8 +21,6 @@ pub fn run() { // structure. // Here is an example of a user data structure. - - let user1 = User { active: true, username: String::from("username123hello"), @@ -35,25 +33,26 @@ pub fn run() { let uemail = String::from("newemail@example.com"); println!("struct mut email: {uemail}"); - let user2 = build_user(String::from("bartal.baek@example.com"), String::from("baraldur12345")); + let user2 = build_user( + String::from("bartal.baek@example.com"), + String::from("baraldur12345"), + ); let u2name = user2.username; let u2email = user2.email; println!("the name of user2 is {u2name}, and the email is {u2email}"); - let update_user1 = User { - - email: String::from("bxtal@example.com"), - ..user1 - }; + email: String::from("bxtal@example.com"), + ..user1 + }; // TUPLE STRUCTS // groupings of related values, where the tuple is named, but the values are unnamed. - let black = Color(0,0,0); - let origin = Point(0,0,0); + let black = Color(0, 0, 0); + let origin = Point(0, 0, 0); - let Point(x,y,z) = origin; + let Point(x, y, z) = origin; println!("the y point is {y} and the x point is {x}, and the z point is {z}"); @@ -65,7 +64,6 @@ pub fn run() { // OWNERSHIP OF STRUCT DATA // Usage of lifetimes, in chap 10, no more explaination here. - } fn build_user(email: String, username: String) -> User { diff --git a/structs/src/main.rs b/structs/src/main.rs index 1cad271..27eb566 100644 --- a/structs/src/main.rs +++ b/structs/src/main.rs @@ -1,6 +1,6 @@ mod defining_structs; -mod struct_program; mod methods; +mod struct_program; fn main() { defining_structs::run(); diff --git a/structs/src/methods.rs b/structs/src/methods.rs index 942d424..05074f2 100644 --- a/structs/src/methods.rs +++ b/structs/src/methods.rs @@ -38,10 +38,7 @@ pub fn run() { }; let sq = Rectangle::square(5); - println!( - "The area of the rectangle is {} square feet", - rect1.area() - ); + println!("The area of the rectangle is {} square feet", rect1.area()); println!("The rectangle has a nonzero width, it is {}", rect1.width()); println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); diff --git a/structs/src/struct_program.rs b/structs/src/struct_program.rs index 3985a28..2012454 100644 --- a/structs/src/struct_program.rs +++ b/structs/src/struct_program.rs @@ -13,11 +13,11 @@ pub fn run() { println!( "The are of the rectangle is {} square pixels", - area(width1,height1) + area(width1, height1) ); // program with struct tuple - let rect1 = (30,50); + let rect1 = (30, 50); println!( "The tuple struct rectancle is {} square pixels", tuple_struct_area(rect1) @@ -32,21 +32,18 @@ pub fn run() { println!( "The struct rectangle is {} square pixels", struct_area(&rect2) -); + ); dbg!(&rect2); println!("this is also debug for rect2\n{rect2:#?}"); println!("this is also debug but not as pretty with :? for rect2\n{rect2:?}"); - - - } fn area(width: u32, height: u32) -> u32 { width * height } -fn tuple_struct_area(dimensions: (u32,u32)) -> u32 { +fn tuple_struct_area(dimensions: (u32, u32)) -> u32 { dimensions.0 * dimensions.1 }