Rust egui Tutorial #2 — Text Widgets: Labels, Headings & More
Video: Rust egui Tutorial #2 — Text Widgets: Labels, Headings & More by Taught by Celeste AI - AI Coding Coach
Watch full page →Rust egui Tutorial #2 — Text Widgets: Labels, Headings & More
In this tutorial, we explore how to display various types of text in an egui application using Rust. You'll learn to create bold headings, simple labels, horizontal separators, and monospace text for code-like output. Additionally, we demonstrate how to use the format! macro to dynamically generate text with variables.
Code
use eframe::egui;
fn ui_example(ui: &mut egui::Ui) {
// Large bold heading text
ui.heading("Welcome to egui Text Widgets");
// Normal read-only label text
ui.label("This is a simple label showing static text.");
// Horizontal separator line
ui.separator();
// Monospace text for code-style display
ui.monospace("let x = 42; // example code snippet");
// Dynamic text using format! macro with variables
let count = 5;
ui.label(format!("You have {} new messages.", count));
}
Key Points
ui.heading()creates large, bold text suitable for titles or section headers.ui.label()displays simple, read-only text labels within the UI.ui.separator()adds a horizontal dividing line to visually separate UI sections.ui.monospace()renders text in a fixed-width font, ideal for showing code snippets.- The
format!macro allows you to build dynamic strings by embedding variables into text.