use std::{fs::{write,File}, io::{Error, Read}};
use clap::{arg, Parser};
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
#[arg(short, long)]
task: String,
#[arg(short, long)]
list: bool,
}
fn main() -> Result<(),Error> {
let arguments = Args::parse();
let mut readbuf = Vec::new();
if arguments.list {
let mut readfl: File = File::open("tasks.txt")?;
readfl.read_to_end(&mut readbuf)?;
println!("{:?}",readbuf);
} else {
let task = arguments.task;
let cloned = task.clone();
write("tasks.txt", task)?;
println!("Inserted task {}",cloned)
}
Ok(())
}
``` I have this code to allow a user to add a task and they can also list the task, but when I test it out, like this:
`rustlearning.exe --task "yo"`
`Inserted task yo`
Which is intended, but when I list the current tasks, i get this:
`[121, 111]`
I might be super dumb here and overlooked something, I just restarted learning rust yesterday, and I'm not finished with the rust book yet.
#Unintended Result in Task CLI
10 messages · Page 1 of 1 (latest)
?play ```rust
println!("{:?}", b"yo");
[121, 111]```
You probably want something like https://doc.rust-lang.org/stable/rust-by-example/std_misc/file/read_lines.html
Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries.
I would also make the type for the Vec explicit, in this case (or have String mentioned somewhere, like it is in that example). That way, you'll get a compile error if it isn't able to do what you want it to do, instead of unexpected behavior at runtime.