this post was submitted on 24 Dec 2024
31 points (100.0% liked)

Rust

6321 readers
9 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

[email protected]

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

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 :)

you are viewing a single comment's thread
view the rest of the comments
[–] kionite231 2 points 1 month ago (8 children)

done :D

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;

    let mut user_inputs: Vec<u32> = Vec::new();
    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();
	user_inputs.push(user_input);
        if x == user_input {
            println!("You won! attempts: {attempts}");
	    println!("Your inputs:");
	    for input in user_inputs {
		print!("{input} ");
	    }
	    println!("");
            break;
        }
        else if x < user_input {
            println!("too big");
            attempts += 1;
        }
        else {
            println!("too small");
            attempts += 1;
        }
        input.clear()
    }
}

[–] [email protected] 1 points 1 month ago (7 children)

@kionite231
You could also color the inputs in different colors

Maybe red if it was wrong and green if the guess was right.

You could also keep that color coding if the list of inputs is getting shown at the end.

[–] kionite231 2 points 1 month ago (6 children)

but to do that I have to use external deps? which I am not comfortable doing. (I am newbie :) )

[–] [email protected] 2 points 1 month ago

@kionite231
Okay I do understand that maybe I will get another idea if so I will let you know.

load more comments (5 replies)
load more comments (5 replies)
load more comments (5 replies)