Time_convert review

https://github.com/DazorPlasma/time_convert

As an exercise to learn Rust, I've made a basic time formatter.
Any criticism is welcomed :innocent:; I need to get rid of bad habits I've picked up coding in other languages.

1 Like

I think you don't need to place this code block in a function:


fn get_input() -> String {
    let mut input: String = String::new();
    std::io::stdin()
        .read_line(&mut input)
        .expect("Couldn't read input!");
    return input;
}

just like this:

fn main() {
    let mut input: String = String::new();
    std::io::stdin()
        .read_line(&mut input)
        .expect("Couldn't read input!");

    let time = match clocktime::parse(&input) {
        Ok(val) => val,
        Err(err) => {
            println!("{} {:?}", "ERROR:".red().bold(), err);
            return;
        }
    };

    println!(
        "24h format: {}",
        time.to_string(clocktime::TimeFormat::TwentyFour)
    );
    println!(
        "12h format: {}",
        time.to_string(clocktime::TimeFormat::Twelve)
    );
}

Also, I recommend you to use the eprintln! macro for printing errors, it does print errors to stderr instead of stdout. And usage of the colored crate seems deprecated, it isn't updated since 2020.

Except for these, I can't see anything wrong in your code.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.