diff --git a/vectors/Cargo.toml b/vectors/Cargo.toml new file mode 100644 index 0000000..d5abdcc --- /dev/null +++ b/vectors/Cargo.toml @@ -0,0 +1,6 @@ +[package] +name = "vectors" +version = "0.1.0" +edition = "2024" + +[dependencies] diff --git a/vectors/src/main.rs b/vectors/src/main.rs new file mode 100644 index 0000000..5cf98e6 --- /dev/null +++ b/vectors/src/main.rs @@ -0,0 +1,88 @@ +use std::io; + +fn main() { + let mut friends: Vec = 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) { + 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 { + 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 +}