oh just cargo fmt it all

This commit is contained in:
2026-10-06 20:58:16 +01:00
parent 37a6496906
commit d154b1e376
17 changed files with 119 additions and 116 deletions
@@ -5,7 +5,9 @@ fn main() {
println!("Please insert a number between 2 and 45"); println!("Please insert a number between 2 and 45");
let mut num = String::new(); 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() { let num: u8 = match num.trim().parse() {
Ok(n) if n < 45 => n, Ok(n) if n < 45 => n,
@@ -19,7 +21,6 @@ fn main() {
println!("{fib}"); println!("{fib}");
} }
fn fibonacci(n: u8) -> u32 { fn fibonacci(n: u8) -> u32 {
if n == 0 { if n == 0 {
0 0
@@ -15,7 +15,6 @@ fn main() {
// then the input should be sanitized and validated / parsed int. // then the input should be sanitized and validated / parsed int.
loop { loop {
println!("Type 1 for converting Celcius to Fahrenheit"); println!("Type 1 for converting Celcius to Fahrenheit");
println!("Type 2 for converting Fahrenheit to Celcius"); println!("Type 2 for converting Fahrenheit to Celcius");
@@ -35,11 +34,9 @@ fn main() {
Err(_) => { Err(_) => {
println!("Please type 1 or 2"); println!("Please type 1 or 2");
continue; continue;
} }
}; };
// TODO USER INPUT 2 // TODO USER INPUT 2
// next up, we need to get a new user input, as float. This must also be sanitized and parsed. // 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, // Here we can perhaps come into some difficulties, what if the user inputs 22? not 22.0? also,
@@ -69,6 +66,5 @@ fn main() {
println!("something went wrong, please try again"); println!("something went wrong, please try again");
continue; continue;
} }
} }
} }
@@ -1,8 +1,18 @@
fn main() { fn main() {
let gifts: [&str;12] = ["A partridge in a pear tree", "Two turtle doves and", "Three french hens ", let gifts: [&str; 12] = [
"Four calling birds", "Five golden rings", "Six geese a-laying", "Seven swans a-swimming", "A partridge in a pear tree",
"Eight maids a-milking", "Nine ladies dancing", "Ten lords a-leaping", "Two turtle doves and",
"Eleven pipers piping", "Twelve drummers drumming" ]; "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]); let mut s = String::from(gifts[0]);
@@ -99,5 +99,4 @@ pub fn run() {
println!("{number_for}"); println!("{number_for}");
} }
println!("for loop LIFTOFF!!!"); println!("for loop LIFTOFF!!!");
} }
+24 -10
View File
@@ -7,7 +7,6 @@ pub fn run() {
println!("delcared floats: \nvar x: {x} \nvar y: {y}"); println!("delcared floats: \nvar x: {x} \nvar y: {y}");
println!("\n=== 2.2 numeric ops ===\n"); println!("\n=== 2.2 numeric ops ===\n");
// addition // addition
let sum = 5 + 10; let sum = 5 + 10;
@@ -24,9 +23,9 @@ pub fn run() {
// remainder // remainder
let remainder = 43 % 5; 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=== 2.2 compound types ===\n");
println!("\n=== tuples ===\n"); println!("\n=== tuples ===\n");
@@ -55,15 +54,30 @@ pub fn run() {
let index_one = tup2.1; let index_one = tup2.1;
let index_two = tup2.2; 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"); println!("\n=== arrays ===\n");
// Arrays can only contain one type. Arrays in rust have a fixed length. // Arrays can only contain one type. Arrays in rust have a fixed length.
// syntax is as such: let a = [1,2,3,4,5]; // 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. // 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. // 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", let months = [
"August", "September", "October", "November", "December"]; "January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
// you can initialize the array with type and length as such: // you can initialize the array with type and length as such:
// let a: [i32;5] = [1,2,3,4,5]; // let a: [i32;5] = [1,2,3,4,5];
@@ -73,9 +87,9 @@ pub fn run() {
let third_month = months[2]; let third_month = months[2];
let fifth_month = months[4]; 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 // Note, the compiler will complain if you try to access a value that is outside the array
} }
+2 -2
View File
@@ -1,7 +1,7 @@
mod variables; mod control_flow;
mod data_types; mod data_types;
mod functions; mod functions;
mod control_flow; mod variables;
fn main() { fn main() {
variables::run(); variables::run();
-4
View File
@@ -6,8 +6,6 @@ enum IpAddrKind {
V6, V6,
} }
// One might try to use an enum and a struct to give it types, like this: // One might try to use an enum and a struct to give it types, like this:
#[derive(Debug)] #[derive(Debug)]
struct IpAddr { struct IpAddr {
@@ -22,7 +20,6 @@ enum IpAddrConcise {
V6(String), V6(String),
} }
pub fn run() { pub fn run() {
println!("\n=== defining enums ===\n"); println!("\n=== defining enums ===\n");
route(IpAddrKind::V4); route(IpAddrKind::V4);
@@ -49,7 +46,6 @@ pub fn run() {
println!("the home address is {home:#?}"); println!("the home address is {home:#?}");
println!("the loopback address is {loopback:#?}"); println!("the loopback address is {loopback:#?}");
} }
fn route(ip_kind: IpAddrKind) { fn route(ip_kind: IpAddrKind) {
+1 -1
View File
@@ -3,7 +3,7 @@ use crate::match_control_flow::UsState::Alabama;
#[derive(Debug)] #[derive(Debug)]
enum UsState { enum UsState {
Alabama, Alabama,
Alaska Alaska,
} }
// Her we define a basic enum // Her we define a basic enum
+2 -4
View File
@@ -17,14 +17,12 @@ pub fn run() {
// This is a string literal. This is hardcoded into the program at compile time. // 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 {
// s is not valid here, since it has not been declared
let s = "hello"; // s is valid from this point onward let s = "hello"; // s is valid from this point onward
println!("{s}"); println!("{s}");
// Do stuff with s // Do stuff with s
} // this scope is now over, and the variable s is no longer valid } // this scope is now over, and the variable s is no longer valid
// when s comes into scope, it is valid // when s comes into scope, it is valid
// when s goes out of scope, it is invalid // when s goes out of scope, it is invalid
} }
+2 -4
View File
@@ -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 // Here is how you can use a variable as a reference to a parameter without taking ownership of
// it // it
// s is a reference to s1 which has a reference to an address in memory // s is a reference to s1 which has a reference to an address in memory
let s1 = String::from("hello"); let s1 = String::from("hello");
let len = calculate_length(&s1); 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. // At any given time, you can have either one mutable reference or any number of immutable references.
// References must always be valid. // 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() s.len()
} // Here, s goes out of scope. But because s does not have ownership of what it refers to } // Here, s goes out of scope. But because s does not have ownership of what it refers to
// The String is not dropped. // 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) { fn change(some_string: &mut String) {
some_string.push_str(", world!"); some_string.push_str(", world!");
} }
-1
View File
@@ -14,7 +14,6 @@ pub fn run() {
let f_word = first_word(&s); let f_word = first_word(&s);
println!("{f_word}"); println!("{f_word}");
let my_string = String::from("hello world"); let my_string = String::from("hello world");
// `first_word` works on slices of `String`s, whether partial or whole. // `first_word` works on slices of `String`s, whether partial or whole.
+4 -6
View File
@@ -21,8 +21,6 @@ pub fn run() {
// structure. // structure.
// Here is an example of a user data structure. // Here is an example of a user data structure.
let user1 = User { let user1 = User {
active: true, active: true,
username: String::from("username123hello"), username: String::from("username123hello"),
@@ -35,14 +33,15 @@ pub fn run() {
let uemail = String::from("newemail@example.com"); let uemail = String::from("newemail@example.com");
println!("struct mut email: {uemail}"); 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 u2name = user2.username;
let u2email = user2.email; let u2email = user2.email;
println!("the name of user2 is {u2name}, and the email is {u2email}"); println!("the name of user2 is {u2name}, and the email is {u2email}");
let update_user1 = User { let update_user1 = User {
email: String::from("bxtal@example.com"), email: String::from("bxtal@example.com"),
..user1 ..user1
}; };
@@ -65,7 +64,6 @@ pub fn run() {
// OWNERSHIP OF STRUCT DATA // OWNERSHIP OF STRUCT DATA
// Usage of lifetimes, in chap 10, no more explaination here. // Usage of lifetimes, in chap 10, no more explaination here.
} }
fn build_user(email: String, username: String) -> User { fn build_user(email: String, username: String) -> User {
+1 -1
View File
@@ -1,6 +1,6 @@
mod defining_structs; mod defining_structs;
mod struct_program;
mod methods; mod methods;
mod struct_program;
fn main() { fn main() {
defining_structs::run(); defining_structs::run();
+1 -4
View File
@@ -38,10 +38,7 @@ pub fn run() {
}; };
let sq = Rectangle::square(5); let sq = Rectangle::square(5);
println!( println!("The area of the rectangle is {} square feet", rect1.area());
"The area of the rectangle is {} square feet",
rect1.area()
);
println!("The rectangle has a nonzero width, it is {}", rect1.width()); println!("The rectangle has a nonzero width, it is {}", rect1.width());
println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2)); println!("Can rect1 hold rect2? {}", rect1.can_hold(&rect2));
-3
View File
@@ -37,9 +37,6 @@ pub fn run() {
dbg!(&rect2); dbg!(&rect2);
println!("this is also debug for rect2\n{rect2:#?}"); println!("this is also debug for rect2\n{rect2:#?}");
println!("this is also debug but not as pretty with :? 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 { fn area(width: u32, height: u32) -> u32 {