gistlib
main.rsuse std::fs::File; use std::io::{self, Write, BufRead, BufReader}; fn main() { let mut tasks: Vec<String> = vec![]; // Load tasks from file let file = File::open("tasks.txt"); if let Ok(file) = file { let reader = BufReader::new(file); for line in reader.lines() { if let Ok(task) = line { tasks.push(task); } } } loop { println!("Todo List"); println!("1. Add Task"); println!("2. View Tasks"); println!("3. Exit"); let choice: i32 = get_user_input("Please enter your choice:"); match choice { 1 => { let task_name = get_user_input("Enter task name:"); tasks.push(task_name); } 2 => { println!("Tasks:"); for (index, task) in tasks.iter().enumerate() { println!("{}. {}", index + 1, task); } } 3 => { // Save tasks to file let mut file = File::create("tasks.txt").expect("Unable to create file"); for task in &tasks { writeln!(file, "{}", task).expect("Unable to write task to file"); } break; } _ => println!("Invalid choice. Please try again."), } } } fn get_user_input(message: &str) -> i32 { println!("{}", message); let mut input = String::new(); io::stdin().read_line(&mut input).expect("Failed to read line"); match input.trim().parse() { Ok(num) => num, Err(_) => { println!("Please enter a valid number."); get_user_input(message) } } } 1755 chars64 lines
use std::fs::File; use std::io::{self, Write, BufRead, BufReader}; fn main() { let mut tasks: Vec<String> = vec![]; // Load tasks from file let file = File::open("tasks.txt"); if let Ok(file) = file { let reader = BufReader::new(file); for line in reader.lines() { if let Ok(task) = line { tasks.push(task); } } } loop { println!("Todo List"); println!("1. Add Task"); println!("2. View Tasks"); println!("3. Exit"); let choice: i32 = get_user_input("Please enter your choice:"); match choice { 1 => { let task_name = get_user_input("Enter task name:"); tasks.push(task_name); } 2 => { println!("Tasks:"); for (index, task) in tasks.iter().enumerate() { println!("{}. {}", index + 1, task); } } 3 => { // Save tasks to file let mut file = File::create("tasks.txt").expect("Unable to create file"); for task in &tasks { writeln!(file, "{}", task).expect("Unable to write task to file"); } break; } _ => println!("Invalid choice. Please try again."), } } } fn get_user_input(message: &str) -> i32 { println!("{}", message); let mut input = String::new(); io::stdin().read_line(&mut input).expect("Failed to read line"); match input.trim().parse() { Ok(num) => num, Err(_) => { println!("Please enter a valid number."); get_user_input(message) } } }
gistlibby LogSnag