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");
|
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,
|
||||||
@@ -60,15 +57,14 @@ fn main() {
|
|||||||
// 2, then f to c. We could have a else to catch something unexpected. Say something like, an
|
// 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...
|
// error occured when trying to convert temperature, please try again...
|
||||||
if temp_type == 1 {
|
if temp_type == 1 {
|
||||||
let result = (degrees * 9.0/5.0) + 32.0;
|
let result = (degrees * 9.0 / 5.0) + 32.0;
|
||||||
println!("{degrees} celcius is {result:.2} fahrenheit");
|
println!("{degrees} celcius is {result:.2} fahrenheit");
|
||||||
} else if temp_type == 2 {
|
} 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");
|
println!("{degrees} fahrenheit is {result:.2} celsius");
|
||||||
} else {
|
} else {
|
||||||
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]);
|
||||||
|
|
||||||
@@ -18,8 +28,8 @@ fn main() {
|
|||||||
println!("{s}");
|
println!("{s}");
|
||||||
|
|
||||||
if day != 12 {
|
if day != 12 {
|
||||||
s.insert_str(0,"\n");
|
s.insert_str(0, "\n");
|
||||||
s.insert_str(0,gifts[day]);
|
s.insert_str(0, gifts[day]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,7 +30,7 @@ pub fn run() {
|
|||||||
|
|
||||||
// You can also use if in a let statements, as such
|
// You can also use if in a let statements, as such
|
||||||
let condition = true;
|
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}");
|
println!("The value of the conditional number is {number}");
|
||||||
|
|
||||||
// repitition with loops
|
// repitition with loops
|
||||||
@@ -55,7 +55,7 @@ pub fn run() {
|
|||||||
// guess. Let's go slowly through this
|
// guess. Let's go slowly through this
|
||||||
|
|
||||||
let mut count = 0;
|
let mut count = 0;
|
||||||
'counting_up: loop{
|
'counting_up: loop {
|
||||||
println!("count = {count}");
|
println!("count = {count}");
|
||||||
let mut remaining = 10;
|
let mut remaining = 10;
|
||||||
|
|
||||||
@@ -99,5 +99,4 @@ pub fn run() {
|
|||||||
println!("{number_for}");
|
println!("{number_for}");
|
||||||
}
|
}
|
||||||
println!("for loop LIFTOFF!!!");
|
println!("for loop LIFTOFF!!!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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();
|
||||||
|
|||||||
@@ -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,9 +46,8 @@ 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) {
|
||||||
println!("your ip kind is {ip_kind:#?}");
|
println!("your ip kind is {ip_kind:#?}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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
|
||||||
|
|||||||
@@ -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
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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!");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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.
|
||||||
|
|||||||
@@ -1,14 +1,14 @@
|
|||||||
#![allow(dead_code)]
|
#![allow(dead_code)]
|
||||||
#![allow(unused_variables)]
|
#![allow(unused_variables)]
|
||||||
struct Color(i32, i32, i32);
|
struct Color(i32, i32, i32);
|
||||||
struct Point(i32, i32, i32);
|
struct Point(i32, i32, i32);
|
||||||
|
|
||||||
struct User {
|
struct User {
|
||||||
active: bool,
|
active: bool,
|
||||||
username: String,
|
username: String,
|
||||||
email: String,
|
email: String,
|
||||||
sign_in_count: u64,
|
sign_in_count: u64,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn run() {
|
pub fn run() {
|
||||||
println!("\n=== defining and instantiating structs ===\n");
|
println!("\n=== defining and instantiating structs ===\n");
|
||||||
@@ -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
|
||||||
};
|
};
|
||||||
@@ -50,10 +49,10 @@ pub fn run() {
|
|||||||
// TUPLE STRUCTS
|
// TUPLE STRUCTS
|
||||||
// groupings of related values, where the tuple is named, but the values are unnamed.
|
// groupings of related values, where the tuple is named, but the values are unnamed.
|
||||||
|
|
||||||
let black = Color(0,0,0);
|
let black = Color(0, 0, 0);
|
||||||
let origin = Point(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}");
|
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
|
// 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
@@ -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();
|
||||||
|
|||||||
@@ -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));
|
||||||
|
|||||||
@@ -13,11 +13,11 @@ pub fn run() {
|
|||||||
|
|
||||||
println!(
|
println!(
|
||||||
"The are of the rectangle is {} square pixels",
|
"The are of the rectangle is {} square pixels",
|
||||||
area(width1,height1)
|
area(width1, height1)
|
||||||
);
|
);
|
||||||
|
|
||||||
// program with struct tuple
|
// program with struct tuple
|
||||||
let rect1 = (30,50);
|
let rect1 = (30, 50);
|
||||||
println!(
|
println!(
|
||||||
"The tuple struct rectancle is {} square pixels",
|
"The tuple struct rectancle is {} square pixels",
|
||||||
tuple_struct_area(rect1)
|
tuple_struct_area(rect1)
|
||||||
@@ -32,21 +32,18 @@ pub fn run() {
|
|||||||
println!(
|
println!(
|
||||||
"The struct rectangle is {} square pixels",
|
"The struct rectangle is {} square pixels",
|
||||||
struct_area(&rect2)
|
struct_area(&rect2)
|
||||||
);
|
);
|
||||||
|
|
||||||
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 {
|
||||||
width * height
|
width * height
|
||||||
}
|
}
|
||||||
|
|
||||||
fn tuple_struct_area(dimensions: (u32,u32)) -> u32 {
|
fn tuple_struct_area(dimensions: (u32, u32)) -> u32 {
|
||||||
dimensions.0 * dimensions.1
|
dimensions.0 * dimensions.1
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user