You've already forked book-exercises
finish vectors
This commit is contained in:
6
friends/Cargo.toml
Normal file
6
friends/Cargo.toml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
[package]
|
||||||
|
name = "vectors"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
88
friends/src/main.rs
Normal file
88
friends/src/main.rs
Normal file
@@ -0,0 +1,88 @@
|
|||||||
|
use std::io;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
let mut friends: Vec<String> = Vec::new();
|
||||||
|
|
||||||
|
friends = init_list();
|
||||||
|
println!("you're friends with {:?}", friends);
|
||||||
|
println!("1. update\n2. quit");
|
||||||
|
|
||||||
|
let mut choice = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut choice)
|
||||||
|
.expect("failed to read line!");
|
||||||
|
|
||||||
|
let choice: u32 = match choice.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if choice == 1 {
|
||||||
|
update_list(&mut friends);
|
||||||
|
println!("updated friends list {:?}", friends);
|
||||||
|
} else {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_list(friends: &mut Vec<String>) {
|
||||||
|
println!("\t...your friends as of now are: {:?}", friends);
|
||||||
|
println!("...which one would you like to update?");
|
||||||
|
|
||||||
|
let mut index = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut index)
|
||||||
|
.expect("failed to read line!");
|
||||||
|
|
||||||
|
let index: usize = match index.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
println!("...and you would like to replace them with: ");
|
||||||
|
let mut to_update_with = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut to_update_with)
|
||||||
|
.expect("failed to read line!");
|
||||||
|
|
||||||
|
friends[index - 1] = to_update_with;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn init_list() -> Vec<String> {
|
||||||
|
println!("how many friends do you have?");
|
||||||
|
|
||||||
|
let mut friend_count = String::new();
|
||||||
|
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut friend_count)
|
||||||
|
.expect("failed to read line!");
|
||||||
|
|
||||||
|
let friend_count: u32 = match friend_count.trim().parse() {
|
||||||
|
Ok(num) => num,
|
||||||
|
Err(_) => {
|
||||||
|
todo!();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
let mut names = Vec::new();
|
||||||
|
|
||||||
|
for i in 0..friend_count {
|
||||||
|
println!("\tenter name of friend {}", i + 1);
|
||||||
|
|
||||||
|
let mut name = String::new();
|
||||||
|
io::stdin()
|
||||||
|
.read_line(&mut name)
|
||||||
|
.expect("failed to read line!");
|
||||||
|
|
||||||
|
names.push(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
names
|
||||||
|
}
|
||||||
@@ -56,6 +56,7 @@ fn main() {
|
|||||||
|
|
||||||
print_result(&result);
|
print_result(&result);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn compare(r1: &Rectangle, r2: &Rectangle, r3: &Rectangle) -> [bool; 6] {
|
fn compare(r1: &Rectangle, r2: &Rectangle, r3: &Rectangle) -> [bool; 6] {
|
||||||
let mut result: [bool; 6] = [false; 6];
|
let mut result: [bool; 6] = [false; 6];
|
||||||
|
|
||||||
|
|||||||
@@ -13,6 +13,9 @@ fn main() {
|
|||||||
print_rules();
|
print_rules();
|
||||||
let tries = manage_balance(load_money());
|
let tries = manage_balance(load_money());
|
||||||
|
|
||||||
|
if tries == 0 {
|
||||||
|
return;
|
||||||
|
} else {
|
||||||
let mut total_payout: u32 = 0;
|
let mut total_payout: u32 = 0;
|
||||||
|
|
||||||
for _ in 0..tries {
|
for _ in 0..tries {
|
||||||
@@ -43,6 +46,7 @@ fn main() {
|
|||||||
}
|
}
|
||||||
println!("\nyou walk away with ${total_payout}");
|
println!("\nyou walk away with ${total_payout}");
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn spin() -> SpinResult {
|
fn spin() -> SpinResult {
|
||||||
let random: u32 = rng().random_range(0..5);
|
let random: u32 = rng().random_range(0..5);
|
||||||
|
|||||||
@@ -35,22 +35,20 @@ fn main() {
|
|||||||
);
|
);
|
||||||
|
|
||||||
println!("\ncaroline replaced kyle");
|
println!("\ncaroline replaced kyle");
|
||||||
|
|
||||||
employees[3] = "caroline";
|
employees[3] = "caroline";
|
||||||
|
|
||||||
println!("employees are: {:?}", employees);
|
println!("employees are: {:?}", employees);
|
||||||
|
|
||||||
employee_of_the_month = (months[1], employees[1], ratings[2]);
|
employee_of_the_month = (months[1], employees[1], ratings[2]);
|
||||||
|
|
||||||
let (x, y, z) = employee_of_the_month;
|
let (x, y, z) = employee_of_the_month;
|
||||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
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]);
|
employee_of_the_month = (months[2], employees[2], ratings[1]);
|
||||||
|
|
||||||
let (x, y, z) = employee_of_the_month;
|
let (x, y, z) = employee_of_the_month;
|
||||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
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]);
|
employee_of_the_month = (months[3], employees[3], ratings[0]);
|
||||||
|
|
||||||
let (x, y, z) = employee_of_the_month;
|
let (x, y, z) = employee_of_the_month;
|
||||||
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
println!("\nin {x}, {y} was the employee of the month with a rating of {z}");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,88 +1,59 @@
|
|||||||
use std::io;
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut friends: Vec<String> = Vec::new();
|
//empty vector must have a type
|
||||||
|
let mut v: Vec<i32> = Vec::new();
|
||||||
|
|
||||||
friends = init_list();
|
//otherwise vectors infer types at the time of assignment
|
||||||
println!("you're friends with {:?}", friends);
|
let mut v1 = vec!["john", "jack", "jacob"];
|
||||||
println!("1. update\n2. quit");
|
|
||||||
|
|
||||||
let mut choice = String::new();
|
v1.push("jill");
|
||||||
|
v.push(2);
|
||||||
|
|
||||||
io::stdin()
|
println!("{:?}, {:?}", v, v1);
|
||||||
.read_line(&mut choice)
|
|
||||||
.expect("failed to read line!");
|
|
||||||
|
|
||||||
let choice: u32 = match choice.trim().parse() {
|
//reading from a vector
|
||||||
Ok(num) => num,
|
//using get and a simple reference
|
||||||
Err(_) => {
|
let fetch = &v[0];
|
||||||
todo!();
|
let fetch1 = v1.get(2);
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
if choice == 1 {
|
println!("{:?}, {:?}", fetch, fetch1);
|
||||||
update_list(&mut friends);
|
|
||||||
println!("updated friends list {:?}", friends);
|
//interating over vector values
|
||||||
} else {
|
for i in &v1 {
|
||||||
return;
|
println!("\t{i}");
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn update_list(friends: &mut Vec<String>) {
|
let mut v2 = vec![10, 20, 30];
|
||||||
println!("\t...your friends as of now are: {:?}", friends);
|
let v2 = add_ten(&mut v2).clone();
|
||||||
println!("...which one would you like to update?");
|
println!("{:?}", v2);
|
||||||
|
|
||||||
let mut index = String::new();
|
using_enums();
|
||||||
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut index)
|
|
||||||
.expect("failed to read line!");
|
|
||||||
|
|
||||||
let index: usize = match index.trim().parse() {
|
|
||||||
Ok(num) => num,
|
|
||||||
Err(_) => {
|
|
||||||
todo!();
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
println!("...and you would like to replace them with: ");
|
|
||||||
let mut to_update_with = String::new();
|
|
||||||
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut to_update_with)
|
|
||||||
.expect("failed to read line!");
|
|
||||||
|
|
||||||
friends[index - 1] = to_update_with;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn init_list() -> Vec<String> {
|
fn add_ten(vec: &mut Vec<i32>) -> &Vec<i32> {
|
||||||
println!("how many friends do you have?");
|
for i in &mut *vec {
|
||||||
|
*i += 10;
|
||||||
let mut friend_count = String::new();
|
|
||||||
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut friend_count)
|
|
||||||
.expect("failed to read line!");
|
|
||||||
|
|
||||||
let friend_count: u32 = match friend_count.trim().parse() {
|
|
||||||
Ok(num) => num,
|
|
||||||
Err(_) => {
|
|
||||||
todo!();
|
|
||||||
}
|
}
|
||||||
};
|
vec
|
||||||
|
|
||||||
let mut names = Vec::new();
|
|
||||||
|
|
||||||
for i in 0..friend_count {
|
|
||||||
println!("\tenter name of friend {}", i + 1);
|
|
||||||
|
|
||||||
let mut name = String::new();
|
|
||||||
io::stdin()
|
|
||||||
.read_line(&mut name)
|
|
||||||
.expect("failed to read line!");
|
|
||||||
|
|
||||||
names.push(name);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
names
|
#[derive(Debug)]
|
||||||
|
enum Groups {
|
||||||
|
Cities(String),
|
||||||
|
NumericCodes(i32),
|
||||||
|
}
|
||||||
|
|
||||||
|
fn using_enums() {
|
||||||
|
let japan = vec![
|
||||||
|
Groups::Cities(String::from("Tokyo")),
|
||||||
|
Groups::NumericCodes(1),
|
||||||
|
];
|
||||||
|
|
||||||
|
let china = vec![
|
||||||
|
Groups::Cities(String::from("Beijing")),
|
||||||
|
Groups::NumericCodes(2),
|
||||||
|
];
|
||||||
|
|
||||||
|
let places = vec![japan, china];
|
||||||
|
|
||||||
|
println!("{:?}", places);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user