0

Hello I'm trying to get user input using the get_input() which will return a String. get_input() is called by check_input() which uses match to check if the input String has only numerals or not and will return a bool depending on the input. I sliced the String and stored it as &str and even then my check_input() function returns false even if the input value was only numerals. Now if I hard code a &str and pass it in the match it returns true.

I'm trying to learn Rust and am very new to the lang (started Rust 3 days ago) so an in-depth explanation on what went wrong would be very welcome. And please give me any critiques/pointers you think one would need to get good at Rust.

CODE:

// // use std::io;                    // to take user input
use std::io;                    // to take user input

fn main() {
//let mut grid_size = get_input().trim().parse::<i64>().unwrap();
//println!("{}", grid_size + 2 );
println!("From main ---> {:?}",checkinput());

 }

fn get_input() -> String{
// fn get_input() -> &'static str{
    println!("Please enter the grid size" );

    let mut input_string = String::new();
    std::io::stdin().read_line(&mut input_string).expect("Failed");
    return input_string;

    // let my_own_str: String = input_string.to_owned();
    // let sliced_str: &str = &my_own_str[..];

    // println!("sliced_str ---> {}\nmy_own_string ---> {}", sliced_str, my_own_str);

    // return sliced_str;

    // let my_test_str: &str = "2";
    // return my_test_str;
}

fn checkinput() -> bool{
    // match get_input().bytes().all(|c| c.is_ascii_digit()) {

    // let  test = get_input().bytes().all(|c| c.is_ascii_digit());
    // let test = get_input().chars().all(char::is_numeric);

    let test_var = get_input(); // i get a String

    let my_own_str: String = test_var.to_owned(); // i own the Strin
    let sliced_str: &str = &my_own_str[..];       // i cut Strin into str
    let sliced_str_new: &str = "123312";       // i cut Strin into str
    // let sliced_str: &str = test_var.as_str();       // i cut Strin into str
    // let sliced_str: &str = "123";       // if i put a str "123" then true

    println!("sliced_str ---> {}", sliced_str); // print to check input val
    println!("my_own_string ---> {}", my_own_str); // print to check input val
    let test = sliced_str.chars().all(char::is_numeric); // check if my str is a numeric

    println!("---------------------------------------");
    println!("Type of my_own_str");
    find_type(&my_own_str);
    println!("---------------------------------------");
    println!("Type of sliced_str");
    find_type(&sliced_str);
    println!("---------------------------------------");
    println!("Type of sliced_str_new");
    find_type(&sliced_str_new);
    println!("---------------------------------------");

    println!("TEST ---> {}", test); // print bool

    match test {
        true => return true,
        false => return false,
    }
}

fn find_type<T>(_: &T) {
    println!("{}", std::any::type_name::<T>())
}
Jmb
  • 18,893
  • 2
  • 28
  • 55
iktefish
  • 19
  • 3

1 Answers1

1

add a .trim() before .chars():

let test = sliced_str.trim().chars().all(char::is_numeric);
Florian Castellane
  • 1,197
  • 2
  • 14
  • 38