Creating beautiful and responsive user interfaces in Rust has always been a challenge until the emergence of egui
. egui
is an immediate mode GUI library that has become a favorite in the Rust community due to its ease of use and cross-platform capabilities. Today, we will delve into the design philosophy, core features, and integration methods of egui
, showcasing its charm in practical application development through examples.
Design Philosophy
The core design philosophy of egui
aims to provide a simple, responsive, portable, and easy-to-integrate GUI library. It is entirely written in Rust and does not use unsafe
code blocks, ensuring safety. egui
can run on the Web and natively on major operating systems and game engines.
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut name);
});
ui.add(egui::Slider::new(&mut age, 0..=120).text("age"));
if ui.button("Increment").clicked() {
age += 1;
}
ui.label(format!("Hello '{}', age {}", name, age));
The code above demonstrates how to create a simple user interface using egui
. We can see that the layout creation is very intuitive, utilizing Rust’s closures to organize the code, making the logic more coherent and easier to understand.
Core Features
egui
provides a range of basic and advanced widgets such as labels, text buttons, hyperlinks, checkboxes, radio buttons, sliders, text editing, and more, all supporting simple immediate interactions. Additionally, it supports image display, custom drawing, multi-line text editing, text selection, and graphics rendering.
Installation and Quick Start
To get started with egui
, you first need to add it to the dependencies in your Cargo.toml
file. Official documentation and examples can be found in the egui
GitHub repository.
To run the Web example, you can use the official template prepared for Web applications, eframe_template
. If you want to integrate egui
into an existing engine, you can explore its rich integration options. For example, it supports integration with libraries and frameworks like winit
, glow
, and WebGPU
.
Easy Integration
Another significant feature of egui
is its ease of integration. Whether embedding it into your favorite game engine or using it in your platform, egui
provides a range of official and third-party integrations. This includes support for integration with game or rendering frameworks such as Amethyst, Bevy, SDL2, glfw, and more.
Sample Projects and Community Support
Want to learn more about the practical applications of egui
? You can refer to its Web examples, run egui
demos online, and experience its smooth interactions and rendering effects. Additionally, you can participate in discussions on egui
‘s GitHub or join the Discord server to engage with the community.
Example: Creating a Simple Interactive Counter
Let’s experience the capabilities of egui
through practice. Here’s a simple example where we will create a button that increments a number:
use eframe::egui::{self, CentralPanel, Context, Layout};
fn main() {
let name = "World";
let mut age = 30;
eframe::run_native(
"My Egui Window",
Default::default(),
Box::new(|cc| Box::new(MyEguiApp::new(cc))),
);
}
struct MyEguiApp {
name: String,
age: u8,
}
impl MyEguiApp {
fn new(_cc: &eframe::CreationContext<'_>) -> Self {
Self {
name: "World".to_owned(),
age: 30,
}
}
fn ui(&mut self, ctx: &Context) {
CentralPanel::default().show(ctx, |ui| {
ui.heading("My egui Application");
ui.horizontal(|ui| {
ui.label("Your name: ");
ui.text_edit_singleline(&mut self.name);
});
ui.add(egui::Slider::new(&mut self.age, 0..=120).text("age"));
if ui.button("Increment").clicked() {
self.age += 1;
}
ui.label(format!("Hello '{}', age {}", self.name, self.age));
});
}
}
impl eframe::App for MyEguiApp {
fn update(&mut self, ctx: &Context, frame: &mut eframe::Frame) {
self.ui(ctx);
}
}
In this example, we created a window containing a text box for editing the name, a slider for selecting age, and a button for incrementing the age. Each time the button is clicked, the age number increments.
Conclusion
egui
brings a fresh breeze to the Rust world with its immediate mode, cross-platform capabilities, simple integration, and ease of use. Whether you’re looking for a concise GUI library for small application development or wish to build a complex user interface running in a game engine, egui
can provide robust support for your project. Its design philosophy, detailed documentation, vivid examples, and active community prove that egui
is an outstanding project worth paying attention to in the Rust ecosystem.
Rust vs C++: A Clash of Modern Programming Languages
Why Now Is the Best Time to Learn Rust
Batch Downloading Files with Rust
Building High-Performance Web Applications with Rust
Getting Started with Rust FFI Development for Python Libraries
Click Follow and Scan to Join the Exchange Group
Get “Rust Language” Learning Materials
Follow the official account and reply “egui” to immediately get the project address.