DEV Community

Cover image for Building a Simple CLI in Rust
Francesco Ciulla
Francesco Ciulla

Posted on

Building a Simple CLI in Rust

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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()
}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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:

Top comments (4)

Collapse
 
maneksoft_texas profile image
Maneksoft

It's an informative post, but could you let me know which programming languages Rust supports or is compatible with?

Collapse
 
francescoxx profile image
Francesco Ciulla

Thank you for your feedback, Maneksoft

Rust is primarily a standalone language, but it’s designed to be highly interoperable with other languages, particularly C and C++.

Using something called Foreign Function Interface (FFI), Rust can call and be called by C code, making it relatively straightforward to integrate Rust into existing C projects or to use C libraries within Rust programs. There are also tools and libraries that help with interoperability between Rust and other languages like Python, JavaScript (via WebAssembly), and more.

If you're looking to use Rust alongside another specific language, feel free to ask for more details!

Collapse
 
maneksoft_texas profile image
Maneksoft

Thanks for your reply. I got it clearly.

Thread Thread
 
francescoxx profile image
Francesco Ciulla

you are welcome