From 04c6e7e66a739eef944d83e1c6513a8dc751073f Mon Sep 17 00:00:00 2001 From: Reality Enjoyer Date: Sat, 20 Dec 2025 18:13:04 +0000 Subject: [PATCH] functions, loops and control flow --- functions/Cargo.toml | 6 +++ functions/src/main.rs | 71 ++++++++++++++++++++++++++++++++++++ loops/Cargo.toml | 6 +++ loops/christmas/Cargo.toml | 6 +++ loops/christmas/src/main.rs | 42 +++++++++++++++++++++ loops/fibonacci/Cargo.toml | 6 +++ loops/fibonacci/src/main.rs | 29 +++++++++++++++ loops/src/main.rs | 17 +++++++++ loops/tempverter/Cargo.toml | 6 +++ loops/tempverter/src/main.rs | 49 +++++++++++++++++++++++++ variables/Cargo.toml | 6 +++ variables/src/main.rs | 56 ++++++++++++++++++++++++++++ 12 files changed, 300 insertions(+) create mode 100644 functions/Cargo.toml create mode 100644 functions/src/main.rs create mode 100644 loops/Cargo.toml create mode 100644 loops/christmas/Cargo.toml create mode 100644 loops/christmas/src/main.rs create mode 100644 loops/fibonacci/Cargo.toml create mode 100644 loops/fibonacci/src/main.rs create mode 100644 loops/src/main.rs create mode 100644 loops/tempverter/Cargo.toml create mode 100644 loops/tempverter/src/main.rs create mode 100644 variables/Cargo.toml create mode 100644 variables/src/main.rs diff --git a/functions/Cargo.toml b/functions/Cargo.toml new file mode 100644 index 0000000..9c392ab --- /dev/null +++ b/functions/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "functions" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/functions/src/main.rs b/functions/src/main.rs new file mode 100644 index 0000000..f5fb34d --- /dev/null +++ b/functions/src/main.rs @@ -0,0 +1,71 @@ +// fn main() { +// print_labelled_measurements(5, 'h'); +// } +// +// fn print_labelled_measurements(value: i32, unit_label: char) { +// println!("The measurement is: {value}{unit_label}"); +// } +// +// // a variable +// +use std::io::{self}; + +fn main() { + println!( + "welcome to our rudimentary calculator!\nwe can do two things:\n1.factorial.2.square\nwhich one do you want?\n\t" + ); + loop { + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("couldn't read line!"); + + let input: i32 = input.trim().parse().expect("numbers only"); + + if input > 2 || input < 0 { + println!("aye choose from the options foo"); + break; + } + + println!("\nalright and what number to operate on?\n\t"); + + let mut number = String::new(); + + io::stdin() + .read_line(&mut number) + .expect("couldn't read line!"); + + let number: i32 = number + .trim() + .parse() + .expect("aye foo enter a numberrr quit playin"); + + if input == 1 { + println!("\nthe factorial of {number} is {}", factorial(number)); + break; + } else { + println!("\nthe square of {number} is {}", square(number)); + break; + } + } + + fn square(x: i32) -> i32 { + return x * x; + } + + //TODO: fix factorial function after you figure out loops + //NOTE: we have implemented factorial in the the loops sub-directory + fn factorial(x: i32) -> i32 { + let mut rev = x - 1; + let mut result = x; + loop { + result *= rev; + rev -= 1; + + if rev < 3 { + break result * 2; + } + } + } +} diff --git a/loops/Cargo.toml b/loops/Cargo.toml new file mode 100644 index 0000000..a046a76 --- /dev/null +++ b/loops/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "loops" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/loops/christmas/Cargo.toml b/loops/christmas/Cargo.toml new file mode 100644 index 0000000..cb4498b --- /dev/null +++ b/loops/christmas/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "christmas" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/loops/christmas/src/main.rs b/loops/christmas/src/main.rs new file mode 100644 index 0000000..9fce1ea --- /dev/null +++ b/loops/christmas/src/main.rs @@ -0,0 +1,42 @@ +//this probably can be made better +fn main() { + let cardinals: [&str; 12] = [ + "first", "second", "third", "fourth", "fifth", "sixth", "seventh", "eighth", "ninth", + "tenth", "eleventh", "twelfth", + ]; + + let lines: [&str; 12] = [ + "A partridge in a pear tree", + "Two turtle doves", + "Three French hens", + "Four colly birds", + "Five gold rings", + "Six geese a-laying", + "Seven swans a-swimming", + "Eight maids a-milking", + "Nine drummers drumming", + "Ten pipers piping", + "Eleven ladies dancing", + "Twelve fiddlers fiddling", + ]; + + for mut i in 1..13 { + println!("The {} day of Christmas,", cardinals[i - 1]); + println!("My true love sent to me"); + + while i != 0 { + while i != 0 { + if i == 1 { + println!("{}.\n", lines[i - 1]); + } else { + if i == 2 { + println!("{}, and", lines[i - 1]); + } else { + println!("{},", lines[i - 1]); + } + } + i -= 1; + } + } + } +} diff --git a/loops/fibonacci/Cargo.toml b/loops/fibonacci/Cargo.toml new file mode 100644 index 0000000..15ac334 --- /dev/null +++ b/loops/fibonacci/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "fibonacci" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/loops/fibonacci/src/main.rs b/loops/fibonacci/src/main.rs new file mode 100644 index 0000000..291dee2 --- /dev/null +++ b/loops/fibonacci/src/main.rs @@ -0,0 +1,29 @@ +use std::io; + +fn fib(x: i32) -> i32 { + if x == 1 { + return 0; + } else if x == 2 { + return 1; + } else { + return fib(x - 1) + fib(x - 2); + } +} + +fn main() { + println!("welcome to the fibo party!"); + println!("which fibonacci number would you like to see?"); + + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("error storing user input"); + + let input: i32 = input + .trim() + .parse() + .expect("error converting to number: did you enter a non-number?"); + + println!("fibonacci number {input} is {}", fib(input)); +} diff --git a/loops/src/main.rs b/loops/src/main.rs new file mode 100644 index 0000000..e054780 --- /dev/null +++ b/loops/src/main.rs @@ -0,0 +1,17 @@ +fn main() { + let fac: i32 = factorial(5); + println!("{fac}"); +} + +fn factorial(x: i32) -> i32 { + let mut rev = x - 1; + let mut result = x; + loop { + result *= rev; + rev -= 1; + + if rev == 1 { + return result; + } + } +} diff --git a/loops/tempverter/Cargo.toml b/loops/tempverter/Cargo.toml new file mode 100644 index 0000000..2162631 --- /dev/null +++ b/loops/tempverter/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "tempverter" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/loops/tempverter/src/main.rs b/loops/tempverter/src/main.rs new file mode 100644 index 0000000..4a6c732 --- /dev/null +++ b/loops/tempverter/src/main.rs @@ -0,0 +1,49 @@ +use std::io; + +fn main() { + println!("welcome to tempverter!"); + println!("please enter your temp:"); + + let mut input = String::new(); + + io::stdin() + .read_line(&mut input) + .expect("error storing user input"); + + let input: i32 = input + .trim() + .parse() + .expect("error storing temperature - did you enter a number?"); + + println!("DEBUG: you entered {input}"); + + println!("is this in C or F?"); + + let mut input_unit = String::new(); + + io::stdin() + .read_line(&mut input_unit) + .expect("error storing user input"); + + let input_unit: char = input_unit + .trim() + .parse() + //NOTE: it won't panic when you enter a number + //because char is an unsigned 32-bit integer + //so it will akshually panic when you enter a negative number + .expect("error storing temperature unit - did you enter a char?"); + + if input_unit == 'c' || input_unit == 'C' { + println!("{input}C is {}F", converter(input, 1)); + } else if input_unit == 'f' || input_unit == 'F' { + println!("{input}F is {}C", converter(input, 2)); + } +} + +fn converter(input: i32, choice: i32) -> i32 { + if choice == 1 { + return 32 + ((9 * input) / 5); + } else { + return ((input - 32) / 9) * 5; + } +} diff --git a/variables/Cargo.toml b/variables/Cargo.toml new file mode 100644 index 0000000..c093eb6 --- /dev/null +++ b/variables/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "variables" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/variables/src/main.rs b/variables/src/main.rs new file mode 100644 index 0000000..266d59b --- /dev/null +++ b/variables/src/main.rs @@ -0,0 +1,56 @@ +// fn main() { +// let mut x = 5; +// println!("The value of x is: {x}"); +// x = 6; +// println!("The value of x is: {x}"); +// } + +// fn main() { +// // let tup: (i32, f64) = (500, 2.91); +// let tup = (500, 2.91); +// let t: bool = true; +// let c: char = '✅'; +// +// println!("t is {t}\nc is {c}\nand tup is {:?}\n\n\n", tup); +// +// // let (x: i32, y: f64, z) = tup; +// let (x, y) = tup; +// println!("the second value is: {}", y); +// println!("the second value is: {}", tup.1); +// } + +fn main() { + let months = ["jan", "feb", "mar", "apr", "may", "jun"]; + let ratings: [i32; 3] = [1, 2, 3]; + let mut employees = ["do kwon", "sbf", "tabasco", "kyle"]; + + println!("employees are: {:?}", employees); + + let mut employee_of_the_month: (&str, &str, i32) = ("jan", "do kwon", 2); + + let (x, y, z) = employee_of_the_month; + println!( + "\nin {x}, {y} was the employee of the month with a rating of {}", + employee_of_the_month.2 + ); + + println!("\ncaroline replaced kyle"); + employees[3] = "caroline"; + + println!("employees are: {:?}", employees); + + employee_of_the_month = (months[1], employees[1], ratings[2]); + + let (x, y, z) = employee_of_the_month; + println!("\nin {x}, {y} was the employee of the month with a rating of {z}"); + + employee_of_the_month = (months[2], employees[2], ratings[1]); + + let (x, y, z) = employee_of_the_month; + println!("\nin {x}, {y} was the employee of the month with a rating of {z}"); + + employee_of_the_month = (months[3], employees[3], ratings[0]); + + let (x, y, z) = employee_of_the_month; + println!("\nin {x}, {y} was the employee of the month with a rating of {z}"); +}