oh just cargo fmt it all
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
@@ -18,8 +28,8 @@ fn main() {
|
||||
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]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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!!!");
|
||||
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
mod variables;
|
||||
mod control_flow;
|
||||
mod data_types;
|
||||
mod functions;
|
||||
mod control_flow;
|
||||
mod variables;
|
||||
|
||||
fn main() {
|
||||
variables::run();
|
||||
|
||||
@@ -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:#?}");
|
||||
}
|
||||
|
||||
@@ -3,7 +3,7 @@ use crate::match_control_flow::UsState::Alabama;
|
||||
#[derive(Debug)]
|
||||
enum UsState {
|
||||
Alabama,
|
||||
Alaska
|
||||
Alaska,
|
||||
}
|
||||
|
||||
// Her we define a basic enum
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,14 +17,12 @@ pub fn run() {
|
||||
|
||||
// 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
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -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!");
|
||||
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
|
||||
@@ -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 {
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
mod defining_structs;
|
||||
mod struct_program;
|
||||
mod methods;
|
||||
mod struct_program;
|
||||
|
||||
fn main() {
|
||||
defining_structs::run();
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user