

Frame Cease-less User-Interactive Concept in Rust Programming
source link: https://blog.knoldus.com/frame-cease-less-user-interactive-concept-in-rust-programming/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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 :
- while
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$
Recommend
-
12
re-frame interactive demo Feb 17, 2019 • Yehonathan Sharvit What is re-frame? re-frame is a functional framework for writing SPAs in ClojureScript, using Reagent. Being...
-
12
PolicyKentucky Issues Cease-and-Desist Order Against Celsius NetworkThe state is the fourth to take legal action against the crypto lender....
-
7
Snap Will Cease Production of its Pixy Drone Due to Worsening Economic Conditions Published Aug. 18, 2022 By
-
7
Lenovo sends cease-and-desist to Framework over a stylized letter “O” Company says Framework's 3D-printed motherboard case infringes its trademark.
-
10
TechCrypto firm FTX gets warning from FDIC to stop ‘misleading’ consumers about depo...
-
6
Tesla sends cease-and-desist to CEO behind video of its cars hitting child-size mannequins He responded by calling Elon Musk a "crybaby" By
-
7
Meta Will Cease Support for its Separate Facebook Gaming App on October 28th Published Aug. 30, 2022 By
-
8
Billionaire investor Bill Ackman joins Elon Musk calling for a Ukraine cease-fire that includes major concessions to RussiaChristiaan HetznerTue, October 18, 2022, 2:56 A...
-
11
robotaxis looking unlikely — Argo AI will cease operations as Ford and Volkswagen pull investments Ford is reallocating those resources to ADAS, VW's CARIAD will work with...
-
6
Posted inNews Feed Warcraft platform to cease operations in China in January by
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK