Hello world
This tutorial will lead you through creating a simple “Hello World” TUI app that displays some text in the middle of the screen and waits for the user to press q to exit. It demonstrates the necessary tasks that any application developed with Ratatui needs to undertake. We assume you have a basic understanding of the terminal, and have a text editor or Rust IDE. If you don’t have a preference, VSCode makes a good default choice.
You’re going to build the following:
The full code for this tutorial is available to view at https://github.com/ratatui/ratatui-website/tree/main/code/hello-ratatui
Install Rust
The first step is to install Rust. See the Installation section of the official Rust Book for more
information. Most people use rustup
, a command line tool for managing Rust versions and associated
tools.
Once you’ve installed Rust, verify it’s installed by running:
You should see output similar to the following (the exact version, date and commit hash will vary):
Install Cargo generate
Cargo generate is a tool that makes it possible to create templates for rust projects.
Install it by running the following command:
See https://cargo-generate.github.io/cargo-generate/installation.html for other approaches to installing cargo-generate.
Create a New Project
Let’s create a new Rust project. In the terminal, navigate to a folder where you will store your projects and run the following command to generate a new app using the simple ratatui template. (You can find more information about this template in the Simple Template README)
You will be prompted for a project name to use. Enter hello-ratatui
.
The cargo generate
command creates a new folder called hello-ratatui
with a basic binary application
in it. If you examine the folders and files created this will look like:
The Cargo.toml
file is filled with some default values and the necessary dependencies (Ratatui
and Crossterm), and one useful dependency (Color-eyre) for nicer error handling.
The generate command created a default main.rs
that runs the app:
And an App
struct in app.rs
that contains the main logic:
The App implementation contains methods to create the app and run the main application loop. The loop runs until the running field is set to false.
The draw method controls what is drawn to the screen. The template draws a paragraph of text surrounded by a borderd block with a title to the entire screen (frame) area.
The app has methods for interacting with the user. These set the running field to false when
the user presses q
, Esc
, or Ctrl+C
Let’s build and execute the project. Run:
You should see the following build messages:
And then the following:
You should then see a TUI app with Hello Ratatui! (press 'q' to quit)
show up in your terminal as
a TUI app.
You can press q
to exit and go back to your terminal as it was before.
Congratulations! 🎉
You have written a “hello world” terminal user interface with Ratatui. The next sections will go into more detail about how Ratatui works.
Next Steps
The next tutorial, Counter App, introduces some more interactivity, and a more robust approach to arranging your application code.