Let’s put a face to the name.
We’ll use egui
to create the interface for our app. egui
is an immediate mode GUI library for Rust.
Here’s a website demonstrating the power of egui
: https://egui.rs
Yupp, the demos run in the browser! Because egui
can compile to WebAssembly. This means that you can build your app for the web and desktop with the same codebase.
Inside quiz/Cargo.toml
, add the following to the [dependencies]
section:
[dependencies]
eframe = "0.24.1"
egui = "0.24.1"
In quiz/src
, replace the main
function with the following:
fn main() -> Result<(), eframe::Error> {
println!("Hello, world!");
let options = eframe::NativeOptions::default();
eframe::run_native("Queeez", options, Box::new(|ctx| Box::new(App::new())))
}
I’ll advise that you don’t stress too much about what all of those mean right now. Label it (in your mind) as boilerplate. In fact, that piece of code is copied from: https://docs.rs/eframe/latest/eframe/#usage-native.
Documentation
In Rust, most of libraries’ documentation are in the form of examples than in natural language. So you should get used to reading examples; and knowing how to find/modify them. A lot of libraries have an examples/
directory in their repository. Always check them out.
App
When you save main.rs
after adding the code above, you should get an error that App
is not defined. Let’s define it:
struct App { }
impl App {
fn new() -> Self {
Self {}
}
}
Now, you should get an error that App
doesn’t implement egui::app::App
. Let’s do that:
// yes, this is another impl block
// in Typescript, this would be: `class App implements eframe::app::App`
impl eframe::App for App {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("Hello world!");
});
}
}
Run your code to see what you’ve done. Forgotten how to run a bin? See Chapter 3: Create project.
UI
Scrap the ui.label("Hello world!");
line. Let’s create our UI:
egui::CentralPanel::default().show(ctx, |ui| {
ui.label("1/10");
ui.label("What is the capital of Nigeria?");
ui.horizontal(|ui| {
ui.button("True");
ui.button("False");
});
ui.horizontal(|ui| {
ui.button("Previous");
ui.button("Next");
});
});
Run the app to see what it looks like: cargo run -p quiz
.
Next we’re going to handle states and logic for our app.