在 Debian 上进行 Rust GUI 开发
一 前置准备
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | shrustup updaterustc --version、cargo --versionsudo apt update && sudo apt install pkg-config二 常见 GUI 框架与选型
三 快速上手示例
sudo apt install libgtk-3-devcargo new rust_gtk_demo && cd rust_gtk_demo[dependencies]
gtk = { version = "0.16", features = ["v3_24"] }
use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button};
fn main() {
let app = Application::builder()
.application_id("com.example.gtk_demo")
.build();
app.connect_activate(|app| {
let window = ApplicationWindow::builder()
.application(app)
.title("Hello, Rust GTK!")
.default_width(400)
.default_height(300)
.build();
let button = Button::with_label("Click Me!");
button.connect_clicked(|_| {
println!("Button clicked!");
});
window.set_child(Some(&button));
window.show();
});
app.run();
}
cargo runsudo apt install mesa-vulkan-driverscargo new egui_demo && cd egui_demo[dependencies]
egui = "0.15"
eframe = "0.15"
use eframe::egui;
struct MyApp { value: i32 }
impl Default for MyApp {
fn default() -> Self { Self { value: 0 } }
}
impl eframe::App for MyApp {
fn update(&mut self, ctx: &egui::Context<'_>) {
egui::Window::new("Hello").show(ctx, |ui| {
ui.label(format!("Value: {}", self.value));
ui.add(egui::Slider::new(&mut self.value, 0..=100).text("value"));
});
}
}
fn main() {
let options = eframe::NativeOptions::default();
eframe::run_native(options, |_cc| Box::new(MyApp::default()));
}
cargo run四 常见问题与排错
sudo apt install libgtk-3-dev,并确保 pkg-config 可用。pkg-config 找不到库路径,可设置 PKG_CONFIG_PATH(如:export PKG_CONFIG_PATH=/usr/lib/x86_64-linux-gnu/pkgconfig)。五 打包与分发
cargo install cargo-bundlecargo bundle --format deb 生成分发包。