Arpith Siromoney 💬

Rendering Text in Rust with Piston-Window

I’m building a browser in Rust at the Recurse Center, and to do that I need to render text! Here’s how you do it with Piston-Window.


Add piston_window and find_folder to your Cargo.toml

piston_window = "*"
find_folder = "*"

Create a window

My browser is full screen!

extern crate piston_window;
extern crate find_folder;

use piston_window::*;

pub fn render(hostname: String) {
    let hostname_str = &hostname[..];
    let mut window: PistonWindow = WindowSettings::new(
            [200, 200]

Use a cool font

find_folder is pretty handy to get your assets folder.

    let assets = find_folder::Search::ParentsThenKids(3, 3)
    let ref font = assets.join("FiraSans-Regular.ttf");
    let factory = window.factory.clone();
    let mut glyphs = Glyphs::new(font, factory).unwrap();

Render your text!

It’s finally time to render your text! Here’s how you do this:

    while let Some(e) = {
        window.draw_2d(&e, |c, g| {
            let transform = c.transform.trans(10.0, 100.0);
            // Set a white background
            clear([1.0, 1.0, 1.0, 1.0], g);
            text::Text::new_color([0.0, 0.0, 0.0, 1.0], 32).draw(
                &mut glyphs,
                transform, g

And that’s it! Building something cool? Get in touch!