You've already forked book-exercises
functions, loops and control flow
This commit is contained in:
6
loops/Cargo.toml
Normal file
6
loops/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "loops"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
6
loops/christmas/Cargo.toml
Normal file
6
loops/christmas/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "christmas"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
42
loops/christmas/src/main.rs
Normal file
42
loops/christmas/src/main.rs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
6
loops/fibonacci/Cargo.toml
Normal file
6
loops/fibonacci/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "fibonacci"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
29
loops/fibonacci/src/main.rs
Normal file
29
loops/fibonacci/src/main.rs
Normal file
@@ -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));
|
||||
}
|
||||
17
loops/src/main.rs
Normal file
17
loops/src/main.rs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
6
loops/tempverter/Cargo.toml
Normal file
6
loops/tempverter/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
||||
[package]
|
||||
name = "tempverter"
|
||||
version = "0.1.0"
|
||||
edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
49
loops/tempverter/src/main.rs
Normal file
49
loops/tempverter/src/main.rs
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user