#Unintended Result in Task CLI

10 messages · Page 1 of 1 (latest)

alpine panther
#
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.
#

It might be that readbuf is automatically type inferred as a Vec<u8>

wild pumice
#

?play ```rust
println!("{:?}", b"yo");

sly shadowBOT
#
[121, 111]```
alpine panther
#

oh

#

how would I convert that to normal text/string?

wild pumice
#

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.

alpine panther
#

ty, i fixed the problem

#

!close