Build Your Text Editor With Rust!

Kofi Otuo
3 min readOct 3, 2021

If you are new to programming or Rust or you just want to learn how to build your text editor but are not sure how to, this tutorial is just right for you.

We will build our editor from scratch so you will get to learn a lot. I’ll explain each step along the way to make understanding a lot easier. It is less than 1000 lines of code and it implements all the basic features you expect in a minimal editor, as well as syntax highlighting and a search feature.

It is in 7 parts: from setup (which is this part) to reading keypress and entering “raw mode”, drawing on the screen and moving the cursor around, displaying text files (making our program a text view), editing text files, and saving changes, implementing a cool search feature and finally to adding syntax highlighting.

Our tutorial is largely based on this tutorial written in C so feel free to check it out.

Now, without further ado, let’s begin.

Setup 🧶

As mentioned, we will write our text editor in Rust so let’s set it up.

Check out the official documentation on how to install rust on your system here. Now it’s time to create a new project.

Creating Cargo project 💼

You can simply use any of the IDEs listed here or the command line. We will use the command line along with Cargo (Rust’s build system and package manager) to build our project. Let’s name our text editor “Pound”.

Open a terminal and enter the following commands to make a projects directory and a directory for the “Pound” project within the projects directory

For Linux, macOS, and PowerShell on Windows, enter this:

$ mkdir ~/projects
$ cd ~/projects
$ cargo new pound
$ cd pound

For Windows CMD, enter this:

> mkdir "%USERPROFILE%\projects"
> cd /d "%USERPROFILE%\projects"
> cargo new pound
> cd pound

The cargo new command creates a new directory called pound. We’ve named our project pound, and Cargo creates its files in a directory of the same name.

Go into the pound directory and list the files. You’ll see that Cargo has generated two files and one directory for us: a Cargo.toml file and an src directory with a main.rs file inside. Now open src/main.rs and take a look

Writing and Running a Rust Program ⚙️

Your main.rs has the following contents by default:

main.rs

The main() function in Rust is special. It is the default starting point when you run your program, so we will put our executable code in the main() function.

To compile and run main.rs, run the cargo run command in your terminal, within the projects directory:

$ cargo run
Compiling pound v0.1.0 (file:///projects/pound)
Finished dev [unoptimized + debuginfo] target(s) in 0.33 secs
Running `target/debug/pound`
Hello, world!

Note ✍️ that cargo run command produces the ‘debug’ version which is suitable for development since all necessary checks are done. The ‘release’ version is generally faster, performs fewer safety checks, and is suitable for the production version. To produce the ‘release’ version simply run the cargo run --release command in your terminal:

$ cargo run --release
Compiling pound v0.1.0 (file:///projects/pound)
Finished release [optimized] target(s) in 1.65s
Running `target/release/pound`
Hello, world!

Now that we have our program working, let’s add one dependency.

Adding dependency 🪢

Recall that Cargo generated a Cargo.toml file:

We will add the crossterm crate (or library). Crossterm contains the various functions and interfaces to interact with the terminal since our text editor interacts with the terminal.

Add the dependency as follows:

cargo.toml

Aaannd that is it for setup … Compile and run your program using cargo run. Cargo would then download the crossterm crate and the program should run fine, producing the Hello, world! output.

Next Steps 👣

In the next part, we’ll work on getting the terminal into raw mode, and reading individual keypresses from the user!

Buy Me A Coffee ☕️

--

--

Kofi Otuo

Sr. Software Engineer in Systems, Mobile and Blockchain programming. I write coding tutorials. Reach Me: https://www.upwork.com/freelancers/~0196d30a485de56f48