hello,

last time I made a fibonacci series generator in Rust and now I have made something different :)

use std::io;

fn main() {
    let mut input: String = String::new();
    let stdin = io::stdin();

    let x = rand::random::<u32>() % 101;
    let mut attempts = 0;

    loop {
        println!("Guess a number from 0 to 100:");
        stdin.read_line(&mut input);
        input = input.to_string().replace("\n", ""); // removing the \n
        let user_input: u32 = input.parse::<u32>().unwrap();
        if x == user_input {
            println!("You won! attempts: {attempts}");
            break;
        }
        else if x < user_input {
            println!("too big");
            attempts += 1;
        }
        else {
            println!("too small");
            attempts += 1;
        }
        input.clear()
    }
}

feel free to give me suggestion :)

  • lad@programming.dev
    link
    fedilink
    English
    arrow-up
    1
    ·
    2 days ago

    I thought, colour codes are platform dependent, will it work on windows

    I usually run things on Linux or macOS, but using a library (crate) may add portability, imo

    • nous@programming.dev
      link
      fedilink
      English
      arrow-up
      2
      ·
      1 day ago

      🤔 I think the vt100 protocols (where the escape code come from) predate windows and I think all modern terminals still use those as the base. So I think they are cross platform. From a quick search it looks like they are the same codes on windows. So I dont think the libraries are doing anything special for cross platform support.

      • lad@programming.dev
        link
        fedilink
        English
        arrow-up
        1
        ·
        1 day ago

        I see, so I was wrong then

        Maybe I should try colour codes on windows when I get to it 😅 thanks for the info