Frame Cease-less User-Interactive Concept in Rust Programming

Reading Time: 3 minutes

Cease-less is the concept of making a program perpetual until the user wants to terminate it, and this concept is implemented by providing a list of option from which the user inputs his/her choice. So, this blog pertains to the building a Menu-Driven program in Rust Programming Langage.

By applying looping in Menu-Driven programming we can achieve the concept of Cease-less program.
* Menu-driven program provides synchronous communication between user and program.
* Looping with Menu-driven makes a program continues running so no need to run the program again and again!!!.
* Menu-driven makes a program user-interactive.

To implement a Cease-less User-interactive program we’ve to follow some steps:
Step 1: Provide a list of options to the user
Step 2: Read the user’s input
Step 3: Wrap the above steps in a loop

Step 1: Provide a list of options to the user

To make a program user interative we have to give a list of option from which user will give his/her input.Options are like:

fn main() {
        println!("Please choose any option!!!");
        println!("Press [1] to choose first option");
        println!("Press [2] to choose second option");
        ...
        println!("Press [n] to Exit");
}

The above code shows the list of options given to the user from which the user will give his/her input.

Step 2: Read the user’s input

Now the second step is to read the user’s input, Rust provides us the standard library(i.e, std) which will give the standard input method(i.e, stdin()) to read the user’s input.

use std::io::stdin;
fn main() {
        println!("Please choose any option!!!");
        ...
        let mut user_input: String = String::new();
        io::stdin().read_line(&mut input);
}

Above code shows the reading of user’s input in a string type variable, so here we get the user’s input from standard input and then with the help of read_line method we write the data into a variable.

read_line(): the signature of this method is [read_line(&mut self, buf: &mut String) -> Result<usize>]
This method takes two arguments the first one is a mutable reference of self and the other is buffer which is the mutable reference of string type.

This method will read bytes from the standard input stream until the newline delimiter or EOF is found. Once found, all bytes up to, and including, the delimiter will be appended to buffer which is a mutable reference of String type.

Step 3: Wrap the above steps in a loop

So, we have provided the list of options and read the user’s input now the last step is wrapping up both the steps in a loop to make a Cease-less user-interactive program.
We have three kinds of loops :

  • loop
  • while
  • for

In perpetual programming, we need an infinite loop so we’ll go for loop keyword.
Using loop keyword Rust provides a way to loop indefinitely until some terminating statement is encountered.

use std::io::stdin;

fn main() {
    loop {
        println!("\nPlease choose any option!!!");
        println!("Press [1] to know about your company");
        println!("Press [2] to Exit");
        let mut input: String = String::new();
        stdin().read_line(&mut input);
        match input.trim() {
            "1" => {
                println!("Enter Your Company Name");
                let mut company_name = String::new();
                stdin().read_line(&mut company_name);
                println!("World's largest pure-play Scala + Spark             
                service company -> {}.", company_name);                           
            }
            "2" => {
                break;
            }
            _ => println!("Please enter valid option"),
        }
    }
}

Above code shows the Cease-less user-interactive menu-driven program.
This program provides two options to the user,
the first option is about the user’s company which takes another user’s input and,
second option is to terminate the program and the default option is fires when the user gives invalid input.

The output of this program is:

 Finished dev [unoptimized + debuginfo] target(s) in 0.01s
     Running `target/debug/user_input`
Please choose any option!!!
Press [1] to know about your company
Press [2] to Exit
1
Please Enter Your Company Name
Knoldus Inc
World's largest pure-play Scala + Spark service company -> Knoldus Inc
Please choose any option!!!
Press [1] to know about your company
Press [2] to Exit
3
Please enter valid option
Please choose any option!!!
Press [1] to know about your company
Press [2] to Exit
2
knoldus@knoldus-Lenovo-g50-80:~/git/knoldus/user_input$ 

Knoldus-blog-footer-image

Written by 

Pawan Singh Bisht is a Software Consultant at Knoldus Software LLP, having a strong experience of more than two years in the technology field. He has been well versed in the core implementation of Rust and Java. He loves to contribute to the community which he attained from the community.

1 thought on “Frame Cease-less User-Interactive Concept in Rust Programming3 min read

  1. I found this post pretty internering. I have just startes with rust and I belive it is great for almost everything, esspecially commandline based programs. I have written a few rust posts myself and I would love it if you could take a look. Think I will take inspiration of your code layout in your posts. Great work!

Comments are closed.

Discover more from Knoldus Blogs

Subscribe now to keep reading and get access to the full archive.

Continue reading