Building a Simple CLI in Rust
Francesco Ciulla
Posted on August 20, 2024
Rust is a powerful and fast systems programming language gaining popularity in various domains.
One interesting aspect of Rust is its capability to create Command Line Interface (CLI) applications.
In this tutorial, we'll build a simple Rust CLI application that takes a string input from the user, reverses it, and prints the reversed string as output.
If you prefer a video version
For a Full Free Rust Course:
Prerequisites
Before we begin, ensure you have the following installed on your system:
- Rust: Install Rust from rust-lang.org.
- Cargo: Cargo is the Rust package manager and is installed automatically with Rust.
To verify that Rust and Cargo are installed, you can run the following commands:
rustc --version
cargo --version
Step 1: Create a New Rust Project
Let's start by creating a new Rust project using Cargo. Open your terminal and run:
cargo new reverse_string_cli
cd reverse_string_cli
This command creates a new directory named reverse_string_cli containing the structure of a basic Rust project.
Step 2: Writing the Code
Open the main.rs file located in the src directory. Replace the content with the following code:
use std::io::{self, Write};
fn main() {
// Prompt the user for input
print!("Enter a string to reverse: ");
io::stdout().flush().unwrap();
// Read the input from the user
let mut input = String::new();
io::stdin().read_line(&mut input).expect("Failed to read line");
// Remove the newline character at the end of the input
input = input.trim().to_string();
// Reverse the string
let reversed = reverse_string(&input);
// Output the reversed string
println!("Reversed string: {}", reversed);
}
fn reverse_string(s: &str) -> String {
s.chars().rev().collect()
}
Explanation:
Prompting the User: We first prompt the user to enter a string using print! and immediately flush the output buffer to ensure the prompt is displayed before waiting for input.
Reading Input: We read the input from the standard input (stdin) and store it in a mutable string (input). We then trim the newline character using trim() and convert it back to a String.
Reversing the String: The reverse_string function takes a string slice (&str) as input, reverses the characters using rev(), and collects them back into a String.
Printing the Reversed String: Finally, we print the reversed string using println!.
Step 3: Build and Run the CLI
To build and run your CLI, use the following commands in your terminal:
cargo build
cargo run
When you run the CLI, it prompts you to enter a string. After you enter the string, it displays its reversed version.
Enter a string to reverse: hello rust
Reversed string: tsur olleh
Conclusion
Congratulations!
In this tutorial, we built a simple CLI application in Rust that takes user input, reverses a string, and returns the reversed string as output.
This project demonstrates the basics of interacting with the user via the command line and manipulating strings in Rust.
You can explore more advanced Rust features to build even more powerful CLI tools from here.
If you prefer a video version
For a Full Free Rust Course:
Posted on August 20, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.