Open In App

Standard I/O in Rust

Last Updated : 17 Mar, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will explore the standard input and standard output in the Rust programming language. Generally, the standard input is provided through the keyboard and the standard output displays values in the console. 

There are two core traits in Rust, around which the Rust’s standard Input and Output are featured. They provide an interface for feeding in input and displaying output. These two traits are listed below:

  • Read Traits
  • Write Traits

Let’s explore them in detail.

Read Trait:

Reading input from an input device in the form of Bytes is done by Rust components called Readers. The read_line() function is used to read data, one line at a time from an input stream or file. Take a look at the below example:

Example:

Rust




use std::io;
 
fn main() {
    println!("Enter a name:");
    let mut guess = String::new();
 
    io::stdin().read_line(&mut guess).expect("failed to readline");
 
    print!("You entered {}", guess);
}


Output:

Same as in other programming languages, we use std::io(standard input/output) library to get input using the read_line function similar to scanf() in C language. The let and mut are keywords to create a mutable variable that can hold the given string.

Write Trait:

The Writers in Rust are programs that can write data to a file or an output stream in bytes. The write() method is used for this purpose. Take a look at the below example.

Example:

Rust




use std::io::Write;
fn main() {
   let var1 = std::io::stdout().write("Geeksforgeeks ".as_bytes()).unwrap();
   let var2 = std::io::stdout().write(
     String::from("is the best.").as_bytes()).unwrap();
   std::io::stdout().write(format!(
     "\n{} bytes of data has been written!",(var1+var2)).as_bytes()).unwrap();
}


Output:

Geeksforgeeks is the best.
26 bytes of data has been written!

In the above example, the write() function is applied to the standard output stream returned by the stdout() standard library function. An enum is returned by the write() method which is further extracted by the unwrap() function to display the result.

 I/O Macros:

The print function is one of the most important and most used to print any output in almost all major programming languages. Rust has and print!() and println!() function similar to printf in C language. We will see how we can print output in Rust. The main difference between Rust’s print! & println! is that the print! outputs in the same line whereas println! give output in a new line.

print!

The print macro function of the program is almost similar to cout in C++ without endl (or) \n. An example of print is given below

Example:

Rust




fn main() {
  
  // printed in new line
  print!("Hello, Geeks");
  print!(" Welcome to GeeksForGeeks");
}


Output:

Hello, Geeks Welcome to GeeksForGeeks

println!

The println is the macro prints to the standard output, with a newline. We can use println! only for the primary output of your program.

Example:

Rust




fn main() {
 
 // printed in new line
  println!("Hello, Geeks");
  println!("Welcome to GeeksForGeeks");
}


Output:

Hello, Geeks
Welcome to GeeksForGeeks

Reading Input

Same as in other programming languages, we use std::io(standard input/output) library to get input using the read_line function similar to scanf() in C language. The let and mut are keywords to create a mutable variable that can hold the given string.

Rust




use std::io;
 
fn main() {
   println!("Enter a name:");
   let mut guess = String::new();
   io::stdin().read_line(&mut guess).expect("failed to readline");
   print!("You entered {}", guess);
}


Output:

Enter a name: geeksforgeeks
You entered geeksforgeeks

CommandLine Arguments:

Command-line arguments are the values passed through the console to the program for processing. It’s similar to how you pass the parameter values to the main() function. The std::env::args() is used to return the command line arguments. Take a look at the below example.

 

Example:

In this example, we will be using command-line arguments to pass values to the main function of the program.

Rust




use std:: env;
 
fn main() {
  let args: Vec<String> = env::args().collect();
   
  for argument in args.iter() {
   
    println!("{}", argument);
  }
}


Let’s check what we did in the above example. First, we need to use the environment module using std:: env. Then we create a new vector of strings. Now, this vector just reads each of our arguments. Meaning we can just say the type of the vector is a string that equals a variable and args.collect() function is used to collect the argument from the command line. Here we made a new vector that holds strings this is equal to all the command-line arguments that are collected

Now use the below command in your shell to activate Rust script:

cargo run

Then pass in the argument as follows:

cargo run YOUR_ARGUMENT

Here we will pass the string “dom” for the sake of example.

Output:



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads