0

I am new to Rust and as an excercise I am trying to make a terminal server apllication. When writing the menu selection section I find that the String variable that i am using to get user input has an "additive behaviour" i.e it does not overwrite the old value with the new one, is there a way to refresh the string without the use of an unsafe approach?

The code is as follows:

//Dependencies
use std::io::stdin;
use std::ffi::OsString;

fn main()
{

    bifrost_menu();

}

fn bifrost_menu()  
{
    //mutable input string
    let mut line = String::new();
    let mut opt = String::new();
    let mut _press = String::new();
    loop 
    {
        //Selection Menu
        println!("                   Bienvenido al menu de Bifrost");
        //selection menu
        println!("Elija una opcion :");
        println!("    1. Acceso a monitor de estado");
        println!("    2. Acceso a dispositivos");
        println!("    3. salir del menu");
    
        //user input
        stdin().read_line(&mut line)
            .ok()
            .expect("Fallo al ingresar opcion");
        //trim aquired string
        if opt.len() >1
        {
            opt.remove(0);
        }
        opt = trim_input (&mut line);
        println!("{} witness print",opt);
        match opt.as_str()
        {
            "1" => {heimdall_menu();},
            "2" => {midgar_menu();},
            "3" => {break},
            _ => {println!("ERROR : por favor ingrese una opcion valida\nPresione cualquier tecla para continuar ...");
                            //user input
                            stdin().read_line(&mut _press)
                                .ok()
                                .expect("Fallo al ingresar opcion");},
        }
        opt = String::new();
    }
    
}

fn heimdall_menu()
{
    //mutable input string
    let mut line = String::new();
    //Selection Menu
    println!("                   Bienvenido al menu de Heimdall");
    //selection menu
    println!("Elija una opcion :");
    println!("    1. Reachability test");
    println!("    2. SNMP quick monitoring");
    println!("    3. Volver al menu anterior");

    //user input
    stdin().read_line(&mut line)
        .ok()
        .expect("Fallo al ingresar opcion");
    //trim aquired string
    let opt = trim_input (&mut line);
}

fn midgar_menu()
{
    //mutable input string
    let mut line = String::new();
    //Selection Menu
    println!("                   Bienvenido al menu de Midgar");

    //selection menu
    println!("Elija una opcion :");
    println!("    1. Acceso a equipos");
    println!("    2. Acceso a backups");
    println!("    3. Volver al menu anterior");
    
    //user input
    stdin().read_line(&mut line)
        .ok()
        .expect("Fallo al ingresar opcion");
    //trim aquired string
    let opt = trim_input (&mut line);

}

fn trim_input (s: &mut String) ->String
{
    if s.ends_with('\n')
    {
        s.pop();
    }
    if s.ends_with('\r')
    {
        s.pop();
    }
    s.starts_with('1');
    return s.to_string();
}

The output of this script is as follows:

~/testfields/rust$ ./input_test 
                   Bienvenido al menu de Bifrost
Elija una opcion :
    1. Acceso a monitor de estado
    2. Acceso a dispositivos
    3. salir del menu
1
1 witness print
                   Bienvenido al menu de Heimdall
Elija una opcion :
    1. Reachability test
    2. SNMP quick monitoring
    3. Volver al menu anterior
2
                   Bienvenido al menu de Bifrost
Elija una opcion :
    1. Acceso a monitor de estado
    2. Acceso a dispositivos
    3. salir del menu
3
13 witness print
ERROR : por favor ingrese una opcion valida
Presione cualquier tecla para continuar ...

The final "witness print" should be "3" not "13" is there a way to overwrite the "1" with the "3"?

3
  • 2
    Can't you just move the line variable inside the loop so that it is created anew each time a new iteration of the loop begins? Why make the variable scope bigger than the loop that actually uses it? Commented Aug 1, 2022 at 20:54
  • wouldn't that just create multiple instances of the same variable? Update: NVM you were right Commented Aug 1, 2022 at 21:00
  • 2
    Does this answer your question? How to overwrite mutable string inside a loop? Commented Aug 2, 2022 at 6:17

1 Answer 1

2

You should simply call line.clear() at the end of each iteration (or at the beginning), which is better than moving the variable inside the loop because doing so will result in more allocations (which are more expensive than clearing a string).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.