jeudi 27 juillet 2017

What is a reasonable String equals check in rust?

I'd like to compare 2 strings in Rust. I'm pretty sure that both "strings" are already strings!

But no matter what the user enters, it always comes the Blacklistmode!

Here is what i've tried:

//Imports
use std::io;

fn main() {
    //Greeting
    println!("[INFO] Welcome to Tracer!\n");
    println!("[ ?? ] Use Blacklist mode ? [Y/n]");

    //Get user input
    let users_choice = get_input();

    //Check if user wants to run in Blacklist Mode
    if users_choice == ToString::to_string("n") || users_choice == ToString::to_string("N") {
        //User wants to use Whitelist mode
        trace_network(0);
    } else {
        //User wants to use Blacklist mode
        trace_network(1);
    }
}

fn get_input() -> String {

    //Creating changeable var
    let mut user_input = String::new();

    //Read Input; On Error drop MSG
    io::stdin().read_line(&mut user_input)
        .expect("[FAIL] Failed to read line");

    println!("[INFO] Your input was: >{}<", user_input);

    return user_input;
}

fn trace_network(mode:i32) {
    println!("[INFO] Starting trace !!! {}", mode);

    //Print wich mode was choosen
    if mode == 0 {
        println!("[INFO] Whitelistmode!");
    } else {
        println!("[INFO] Blacklistmode!");
    }
}

Aucun commentaire:

Enregistrer un commentaire