1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites & Requirements)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Windows 10/11 64-bit, macOS 13+ (Ventura), Linux (glibc 2.31+) | Windows 11, macOS 15+ (Apple Silicon), Ubuntu 22.04/24.04 LTS | Android/iOS ๋ชจ๋ฐ์ผ ๋น๋๋ Android NDK ๋๋ macOS Xcode ํ์ |
| CPU | 4 ์ฝ์ด ์ด์ (Intel Core i5 / AMD Ryzen 5 / Apple Silicon M1) | 8 ์ฝ์ด ์ด์ (Apple M2/M3/M4, AMD Ryzen 7, Intel i7+) | Rust ์ฆ๋ถ ์ปดํ์ผ ๋ฐ Wasm ์ต์ ํ(wasm-opt) ์๋์ ์ง์ ์ ์ํฅ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 8 GB ์ด์ | 16 GB ~ 32 GB ์ด์ | cargo ๋ณ๋ ฌ ๋น๋ ๋ฐ ์ปดํ์ผ๋ฌ ๋ฉ๋ชจ๋ฆฌ ์ ์ ๋๋น ๊ถ์ฅ |
| ๋์คํฌ (Storage) | 10 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 30 GB ์ด์์ ๊ณ ์ NVMe SSD | WebAssembly ํ๊น ๋ฐ target/ ๋น๋ ์ํฐํฉํธ ์บ์ ๊ณต๊ฐ ํ๋ณด |
| ๋ฐํ์ / ํด์ฒด์ธ | Rust 1.82.0+, wasm32-unknown-unknown ํ๊น | Rust 1.85+ (2024 Edition), cargo-binstall, dioxus-cli (dx 0.6+) | Web ํ๊น ๋น๋ ์ WebAssembly ํด์ฒด์ธ ํ์ |
๐ ๏ธ ๊ฐ๋ฐ ํ๊ฒฝ ๊ตฌ์ถ ๋ฐ CLI ์ค์น
Dioxus 0.6 ๊ฐ๋ฐ์ ์ํด์๋ Rust ์ต์ Stable ํด์ฒด์ธ, Wasm ๋น๋ ํ๊น, ๊ทธ๋ฆฌ๊ณ Dioxus ์ ์ฉ ๋น๋ ๋ฐ ํซ ๋ฆฌ๋ก๋ ๋๊ตฌ์ธ **dx (Dioxus CLI)**๊ฐ ํ์์ ์
๋๋ค.
1. Rust ํด์ฒด์ธ ๋ฐ Wasm ํ๊น ์ค์น
# 1. Rust ์ต์ Stable ํด์ฒด์ธ ์
๋ฐ์ดํธ
rustup update stable
rustup default stable
# 2. Web (WebAssembly) ๋น๋๋ฅผ ์ํ ํ๊น ์ถ๊ฐ (ํ์)
rustup target add wasm32-unknown-unknown
2. Dioxus CLI (dx) ์ค์น
dx๋ ํ๋ก์ ํธ ์์ฑ(dx new), ํซ ๋ฆฌ๋ก๋ ๊ฐ๋ฐ ์๋ฒ ์คํ(dx serve), ํ๋ซํผ๋ณ ํจํค์ง(dx bundle)์ ์ง์ํ๋ ์ฌ์ธ์ ๋๊ตฌ์
๋๋ค.
# ๋ฐฉ๋ฒ A: cargo-binstall์ ํตํ ์ฌ์ ์ปดํ์ผ ๋ฐ์ด๋๋ฆฌ ์ด๊ณ ์ ์ค์น (๊ฐ๋ ฅ ๊ถ์ฅ)
cargo install cargo-binstall
cargo binstall dioxus-cli
# ๋ฐฉ๋ฒ B: ๊ธฐ๋ณธ cargo ์์ค ๋น๋ ์ค์น (์๊ฐ์ด ๋ค์ ์์๋จ)
cargo install dioxus-cli --locked
# ์ค์น ์๋ฃ ํ์ธ
dx --version
# ์ถ๋ ฅ ์: dioxus 0.6.x
3. OS๋ณ ๋ค์ดํฐ๋ธ ๋ฐ์คํฌํฑ ๋น๋ ์ข ์์ฑ
๋ฐ์คํฌํฑ ์ ํ๋ฆฌ์ผ์ด์
(--platform desktop)์ ๊ฐ๋ฐํ ๊ฒฝ์ฐ ๊ฐ OS์ ๋ค์ดํฐ๋ธ ์น๋ทฐ ์์ง์ ์ ๊ทผํ๊ธฐ ์ํ ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ํ์ํฉ๋๋ค.
- ๐ช Windows:
- Visual Studio 2022 C++ ๋น๋ ๋๊ตฌ (ใC++๋ฅผ ์ฌ์ฉํ ๋ฐ์คํฌํฑ ๊ฐ๋ฐใ ์ํฌ๋ก๋ ํ์)
- WebView2 Runtime (Windows 10/11์ ๊ธฐ๋ณธ ๋ด์ฅ)
- ๐ macOS:
- Xcode Command Line Tools (
xcode-select --install)
- Xcode Command Line Tools (
- ๐ง Linux (Ubuntu / Debian):
sudo apt update && sudo apt install -y \ libwebkit2gtk-4.1-dev \ build-essential \ curl \ wget \ file \ libxdo-dev \ libssl-dev \ libayatana-appindicator3-dev \ librsvg2-dev
2. Dioxus 0.6 ํต์ฌ ์ํคํ ์ฒ & ๋ฉํ ๋ชจ๋ธ (Mental Model)
Dioxus๋ React์ ์ง๊ด์ ์ธ ์ ์ธํ UI ํจ๋ฌ๋ค์๊ณผ Rust์ ๊ณ ์ฑ๋ฅยท๋ฉ๋ชจ๋ฆฌ ๋ฌด๊ฒฐ์ฑ์ ๊ฒฐํฉํ ํฌ๋ก์คํ๋ซํผ ํ๋ ์์ํฌ์ ๋๋ค. Dioxus 0.6๋ถํฐ๋ Signals 2.0 ์์ง์ด ํ์ฌ๋์ด ๊ฐ์ DOM(VDOM) ์ฌ๋ ๋๋ง ๋น์ฉ์ ์ต์ํํ๊ณ ๊ทน๋๋ก ์ธ๋ถํ๋ ๋ฐ์ํ ๊ฐฑ์ ์ ์ํํฉ๋๋ค.
+-----------------------------------------------------------------------------------+
| Dioxus Component UI (RSX Declarative Tree) |
| rsx! { |
| div { class: "card", |
| h1 { "์นด์ดํฐ: {count}" }, |
| button { onclick: move |_| count += 1, "์ฆ๊ฐ" } |
| } |
| } |
+-----------------------------------------------------------------------------------+
โฒ
Signals 2.0 Reactive Runtime (์ธ๋ถํ๋ ๋ฐ์ํ ๊ตฌ๋
)
โผ
+-----------------------------------------------------------------------------------+
| Dioxus Virtual DOM & Mutations Diffing |
| - ์ปดํฌ๋ํธ ์ ์ฒด๊ฐ ์๋ ๊ฐ์ด ๋ฐ๋ ์๋ธํธ๋ฆฌ ๋
ธ๋๋ง ์ ๋ฐ ์๋ณ |
+-----------------------------------------------------------------------------------+
โฒ
Pluggable Multi-Platform Renderers (๋จ์ผ ์ฝ๋๋ฒ ์ด์ค)
โผ
+---------------------+---------------------+------------------+--------------------+
| Web (Wasm) | Desktop (WebView) | Mobile (Native) | Fullstack (SSR/Fn) |
| - wasm-bindgen | - Wry / Tao | - iOS WKWebView | - Axum / Warp ๊ธฐ๋ฐ |
| - ๋ธ๋ผ์ฐ์ ์ง์ DOM| - ๊ฒฝ๋ ๋ค์ดํฐ๋ธ ์ฐฝ | - Android AOSP | - #[server] RPC ํต์ |
+---------------------+---------------------+------------------+--------------------+
๐ฅ ๊ธฐ์ ๋ณ ํต์ฌ ํน์ง ๋น๊ต
| ๋น๊ต ํญ๋ชฉ | Dioxus 0.6 | Tauri 2.0 | Flutter | React / Next.js |
|---|---|---|---|---|
| ์ฃผ ํ๋ก๊ทธ๋๋ฐ ์ธ์ด | Rust (๋จ์ผ ์ธ์ด ํ์คํ) | Rust (๋ฐฑ์๋) + JS/TS (UI) | Dart | TypeScript / JavaScript |
| UI ์์ฑ ๋ฐฉ์ | RSX ๋งคํฌ๋ก (Rust ์ ์ธํ) | HTML / CSS / JS (React ๋ฑ) | Dart ์์ ฏ ํธ๋ฆฌ | JSX / TSX |
| ๋ฐ์ํ ์ํ ๊ด๋ฆฌ | Signals 2.0 (use_signal) | JS ํ๋ ์์ํฌ ์ํ ๊ด๋ฆฌ | setState, Riverpod, Bloc | useState, Zustand |
| ์คํ ์์ง (Web) | WebAssembly (Wasm) | JS ๋ฒ๋ค | CanvasKit / HTML Wasm | JS V8 ์์ง |
| ํ์คํ ์๋ฒ ํจ์ | ์ง์ (#[server] RPC ๋ด์ฅ) | ๋ฏธ์ง์ (IPC ์ปค๋งจ๋ ๋ฐฉ์) | ๋ฏธ์ง์ | ์ง์ (Server Actions) |
| ๋ฉ๋ชจ๋ฆฌ ์์ ์ฑ | Rust ๋ณด์ฅ (GC ๋ถํ ์ ๋ก) | Rust(Core) + JS(UI) ํผํฉ | Dart GC ๊ฐ๋น์ง ์ปฌ๋ ํฐ | JS V8 GC ๊ฐ๋น์ง ์ปฌ๋ ํฐ |
3. [1๋จ๊ณ] ํฌ๋ก์๋ & ์ฒซ Dioxus ํ๋ก์ ํธ (Step-by-Step)
Dioxus CLI(dx)๋ฅผ ์ฌ์ฉํ์ฌ ํ
ํ๋ฆฟ ํ๋ก์ ํธ๋ฅผ ์์ฑํ๊ณ ๋ก์ปฌ ๊ฐ๋ฐ ์๋ฒ๋ฅผ ๊ตฌ๋ํฉ๋๋ค.
๐ 1. ์ ๊ท ํ๋ก์ ํธ ์์ฑ
# ๋ํํ CLI ๋ง๋ฒ์ฌ๋ก ํ๋ก์ ํธ ์์ฑ
dx new my-dioxus-app
- ๋ง๋ฒ์ฌ ์ ํ ๊ฐ์ด๋:
- Platform:
Web(๋๋Desktop,Fullstack) - Router:
Yes(๊ถ์ฅ) - Styling:
Tailwind๋๋Vanilla CSS
- Platform:
๐ฆ 2. Cargo.toml ๊ตฌ์ฑ ์ดํด๋ณด๊ธฐ
์๋์ผ๋ก ํ๋ก์ ํธ๋ฅผ ๊ตฌ์ฑํ ๊ฒฝ์ฐ ๋ค์ Cargo.toml ์์กด์ฑ์ ์ ์ธํฉ๋๋ค:
[package]
name = "my-dioxus-app"
version = "0.1.0"
edition = "2021"
[dependencies]
# Dioxus 0.6 ํต์ฌ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ๋ฐ ์น ํ๊น
dioxus = { version = "0.6", features = ["web", "router"] }
# ๋ก๊น
์ง์
tracing = "0.1"
tracing-wasm = "0.2"
๐ป 3. src/main.rs ์ต์ ๋์ ์ฝ๋
Dioxus ์ปดํฌ๋ํธ๋ ์ผ๋ฐ์ ์ธ Rust ํจ์์ #[component] ๋งคํฌ๋ก๋ฅผ ๋ถ์ด๊ณ Element๋ฅผ ๋ฐํํ๋ ํํ๋ก ์์ฑํฉ๋๋ค.
use dioxus::prelude::*;
fn main() {
// ์น ํ๊ฒฝ์์๋ ๋ธ๋ผ์ฐ์ ์ฝ์ ๋ก๊ฑฐ ์ด๊ธฐํ
tracing_wasm::set_as_global_default();
// Dioxus ์น ์ ํ๋ฆฌ์ผ์ด์
์์
dioxus::launch(App);
}
#[component]
fn App() -> Element {
// 0.6 ์ ๊ท Signals: use_signal์ ํตํ ๋ฐ์ํ ์นด์ดํฐ ์ํ ์ ์ธ
let mut count = use_signal(|| 0);
rsx! {
div {
style: "display: flex; flex-direction: column; align-items: center; justify-content: center; min-height: 100vh; font-family: sans-serif; background-color: #0f172a; color: #f8fafc;",
h1 {
style: "font-size: 2.5rem; margin-bottom: 0.5rem; color: #38bdf8;",
"๐ฆ Hello Dioxus 0.6!"
}
p {
style: "font-size: 1.1rem; color: #94a3b8; margin-bottom: 2rem;",
"Rust๋ก ๊ตฌ์ถํ๋ ์ด๊ณ ์ WebAssembly ํฌ๋ก์คํ๋ซํผ ์ฑ"
}
div {
style: "background: #1e293b; padding: 2rem; border-radius: 12px; box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.3); text-align: center; border: 1px solid #334155;",
h2 {
style: "font-size: 1.5rem; margin-bottom: 1.5rem;",
// Dioxus 0.6์์๋ count() ํธ์ถ ๋๋ "{count}" ํ์์ผ๋ก ๋ฐ๋ก ํฌ๋งทํ
๊ฐ๋ฅ
"ํ์ฌ ์นด์ดํธ: {count}"
}
div {
style: "display: flex; gap: 0.75rem; justify-content: center;",
button {
style: "padding: 0.6rem 1.2rem; font-size: 1rem; font-weight: bold; background: #ef4444; color: white; border: none; border-radius: 6px; cursor: pointer;",
// Dioxus 0.6์์๋ count -= 1 ๋ก ์ง๊ด์ ์ธ ์ฐ๊ธฐ ๊ฐ๋ฅ
onclick: move |_| count -= 1,
"๊ฐ์ (-1)"
}
button {
style: "padding: 0.6rem 1.2rem; font-size: 1rem; font-weight: bold; background: #64748b; color: white; border: none; border-radius: 6px; cursor: pointer;",
onclick: move |_| count.set(0),
"์ด๊ธฐํ"
}
button {
style: "padding: 0.6rem 1.2rem; font-size: 1rem; font-weight: bold; background: #10b981; color: white; border: none; border-radius: 6px; cursor: pointer;",
// Dioxus 0.6์์๋ count += 1 ๋ก ์ง๊ด์ ์ธ ์ฐ๊ธฐ ๊ฐ๋ฅ
onclick: move |_| count += 1,
"์ฆ๊ฐ (+1)"
}
}
}
}
}
}
โก 4. ํซ ๋ฆฌ๋ก๋(Hot Reload) ๊ฐ๋ฐ ์๋ฒ ๊ตฌ๋
# Web ํ๊น์ผ๋ก ๋ก์ปฌ ๊ฐ๋ฐ ์๋ฒ ์คํ (๋ธ๋ผ์ฐ์ ์๋ ์คํ & RSX ์ค์๊ฐ ํซ ๋ฆฌ๋ก๋)
dx serve --platform web
# ๋ฐ์คํฌํฑ ๋ค์ดํฐ๋ธ ์๋์ฐ๋ก ์คํํ ๊ฒฝ์ฐ
dx serve --platform desktop
- ์์ ์คํ ๊ฒฐ๊ณผ:
- ๋ธ๋ผ์ฐ์ (
http://127.0.0.1:8080)๊ฐ ์๋์ผ๋ก ์ด๋ฆฝ๋๋ค. - ์นด์ดํฐ ๋ฒํผ ํด๋ฆญ ์ ์ ์ฒด ํ์ด์ง ๊น๋นก์ ์์ด ์ซ์ ํ ์คํธ๋ง ์ด๊ณ ์์ผ๋ก ์ค์๊ฐ ๊ฐฑ์ ๋ฉ๋๋ค.
rsx!๋ด๋ถ์ ํ ์คํธ๋ ์คํ์ผ์ ์์ ํ๊ณ ์ ์ฅํ๋ฉด, Rust ์ฌ์ปดํ์ผ ์์ด 0.1์ด ๋ด์ ํซ ๋ฆฌ๋ก๋๊ฐ ์ ์ฉ๋ฉ๋๋ค.
- ๋ธ๋ผ์ฐ์ (
4. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ & ์ํ ๊ด๋ฆฌ (Core Fundamentals)
Dioxus 0.6์ ํต์ฌ์ธ Signals 2.0, ์ปดํฌ๋ํธ Props, ๋ฐ๋ณต/์กฐ๊ฑด ๋ ๋๋ง, ํผ ์ ๋ ฅ ๋ฐ์ธ๋ฉ์ ๋ง์คํฐํฉ๋๋ค.
๐ 1. Signals 2.0 ๋ฐ์ํ ์ํ ๋ฌธ๋ฒ
Dioxus 0.6์์๋ ์ด์ ๋ฒ์ ์ ๋ฒ๊ฑฐ๋ก์ด use_state๊ฐ ์์ ์ ๊ฑฐ๋๊ณ ์ ์ญ ๋ฐ ์ง์ญ ์ํ ๋ชจ๋ **use_signal**๋ก ํต์ผ๋์์ต๋๋ค.
use dioxus::prelude::*;
#[component]
fn SignalDemo() -> Element {
let mut name = use_signal(|| String::from("Ferris"));
let mut score = use_signal(|| 100);
// 1. ์ฝ๊ธฐ (Read):
// ํจ์ ํธ์ถ ๋ฐฉ์: score()
// ๋ฌธ์์ด ๋ณด๊ฐ: "{score}"
// ๋ณต์ฌ ๋๋ ์ฐธ์กฐ ์ทจ๋: score.read()
let current_score = score();
// 2. ์ฐ๊ธฐ (Write):
// ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ: score += 10
// ๊ฐ ๊ต์ฒด: score.set(200)
// ๊ฐ๋ณ ์ฐธ์กฐ ๋์ฌ: score.write().push_str(" the Crab")
rsx! {
div {
p { "์ด๋ฆ: {name}" }
p { "์ ์: {current_score}" }
button {
onclick: move |_| {
score += 5;
name.write().push('!');
},
"์ ์ ์ฌ๋ฆฌ๊ธฐ & ์ด๋ฆ์ ๋๋ํ ์ถ๊ฐ"
}
}
}
}
๐งฉ 2. ์ปดํฌ๋ํธ ๋ถ๋ฆฌ ๋ฐ Props ์ ๋ฌ
Dioxus์ #[component] ๋งคํฌ๋ก๋ ํจ์์ ์ธ์๋ฅผ Props ๊ตฌ์กฐ์ฒด๋ก ์๋ ๋ณํํฉ๋๋ค. ReadOnlySignal์ ํตํด ๋ถ๋ชจ์ ์ํ๋ฅผ ๋ฐ์ํ์ผ๋ก ์ ๋ฌ๋ฐ์ ์ ์์ต๋๋ค.
use dioxus::prelude::*;
// ์์ ์ปดํฌ๋ํธ ์ ์
#[component]
fn UserCard(
username: String,
role: String,
score: ReadOnlySignal<i32>,
on_boost: EventHandler<i32>,
) -> Element {
rsx! {
div { class: "user-card", style: "border: 1px solid #475569; padding: 1rem; border-radius: 8px; margin: 0.5rem 0;",
h3 { "{username} ({role})" }
p { "ํ์ฌ ํ๋ ์ ์: {score}" }
button {
style: "background: #3b82f6; color: white; padding: 0.4rem 0.8rem; border-radius: 4px; border: none; cursor: pointer;",
// ๋ถ๋ชจ์๊ฒ ์ด๋ฒคํธ ํต์ง (๊ฐ 10 ์ ๋ฌ)
onclick: move |_| on_boost.call(10),
"๋ถ์คํธ +10 ์ "
}
}
}
}
// ๋ถ๋ชจ ์ปดํฌ๋ํธ
#[component]
fn UserManager() -> Element {
let mut my_score = use_signal(|| 50);
rsx! {
div {
h2 { "์ฌ์ฉ์ ๋์๋ณด๋" }
UserCard {
username: "ํ๊ธธ๋".to_string(),
role: "๊ด๋ฆฌ์".to_string(),
score: my_score,
// ์์์ EventHandler ์ฝ๋ฐฑ ์ฒ๋ฆฌ
on_boost: move |boost_val| {
my_score += boost_val;
}
}
}
}
}
๐ 3. ์กฐ๊ฑด๋ถ ๋ ๋๋ง & ๋ฆฌ์คํธ ๋ฐ๋ณต๋ฌธ
rsx! ๋งคํฌ๋ก ์์์๋ ์ผ๋ฐ Rust์ if, match, for ๋ฌธ์ ์ง๊ด์ ์ผ๋ก ์ฌ์ฉํ ์ ์์ต๋๋ค. ๋ฆฌ์คํธ ๋ ๋๋ง ์์๋ ์ฑ๋ฅ์ ์ํด ๋ฐ๋์ ๊ณ ์ ํ key๋ฅผ ์ง์ ํด์ผ ํฉ๋๋ค.
use dioxus::prelude::*;
#[derive(Clone, PartialEq)]
struct TodoItem {
id: usize,
title: String,
completed: bool,
}
#[component]
fn TodoList() -> Element {
let mut todos = use_signal(|| vec![
TodoItem { id: 1, title: "Dioxus 0.6 ํํ ๋ฆฌ์ผ ์ฝ๊ธฐ".to_string(), completed: true },
TodoItem { id: 2, title: "Signals ๋ฐ์ํ ์์ ์์ฑ".to_string(), completed: false },
TodoItem { id: 3, title: "Wasm ๋ฒ๋ค ๋น๋ ํ
์คํธ".to_string(), completed: false },
]);
let mut new_title = use_signal(String::new);
rsx! {
div { style: "max-width: 500px; margin: 2rem auto; font-family: sans-serif;",
h2 { "๐ Dioxus Todo ๋ชฉ๋ก" }
// ์
๋ ฅ ํผ
div { style: "display: flex; gap: 0.5rem; margin-bottom: 1rem;",
input {
style: "flex: 1; padding: 0.5rem; border: 1px solid #cbd5e1; border-radius: 4px;",
placeholder: "์ ํ ์ผ์ ์
๋ ฅํ์ธ์...",
value: "{new_title}",
oninput: move |evt| new_title.set(evt.value())
}
button {
style: "background: #0ea5e9; color: white; border: none; padding: 0.5rem 1rem; border-radius: 4px; cursor: pointer;",
onclick: move |_| {
let title = new_title();
if !title.trim().is_empty() {
let next_id = todos.read().len() + 1;
todos.write().push(TodoItem {
id: next_id,
title: title.trim().to_string(),
completed: false,
});
new_title.set(String::new());
}
},
"์ถ๊ฐ"
}
}
// ๋ฆฌ์คํธ ๋ฐ๋ณต ๋ ๋๋ง
ul { style: "list-style: none; padding: 0;",
for item in todos() {
li {
key: "{item.id}", // VDOM ์ต์ ํ๋ฅผ ์ํ ๊ณ ์ ํค ์ง์ ํ์
style: "display: flex; align-items: center; justify-content: space-between; padding: 0.6rem; border-bottom: 1px solid #f1f5f9;",
span {
style: if item.completed { "text-decoration: line-through; color: #94a3b8;" } else { "color: #1e293b;" },
"{item.title}"
}
div {
button {
style: "margin-left: 0.5rem; padding: 0.2rem 0.5rem; font-size: 0.8rem; cursor: pointer;",
onclick: move |_| {
// ํด๋น ์์ดํ
์ completed ์ํ ํ ๊ธ
let mut list = todos.write();
if let Some(target) = list.iter_mut().find(|t| t.id == item.id) {
target.completed = !target.completed;
}
},
if item.completed { "์ทจ์" } else { "์๋ฃ" }
}
}
}
}
}
}
}
}
5. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ ํจํด (Advanced Real-World Patterns)
์ค๋ฌด ํ๋ก๋์ ์ ํ๋ฆฌ์ผ์ด์ ๊ฐ๋ฐ์ ํ์์ ์ธ ๋น๋๊ธฐ API ํ์นญ, ํ์ ์์ ๋ผ์ฐํฐ, ํ์คํ ์๋ฒ ํจ์(Server Functions), ์ ์ญ ์ปจํ ์คํธ๋ฅผ ๊ตฌํํฉ๋๋ค.
๐ 1. use_resource๋ฅผ ์ด์ฉํ ๋น๋๊ธฐ ๋ฐ์ดํฐ ํ์นญ
use_resource๋ ๋น๋๊ธฐ ์์
(HTTP ํธ์ถ ๋ฑ)์ ์ฒ๋ฆฌํ๊ณ ๋ก๋ฉ, ์ฑ๊ณต, ์คํจ ์ํ๋ฅผ ์๋์ผ๋ก ๊ด๋ฆฌํ๋ ํ
์
๋๋ค. ๋ด๋ถ์์ ์ฐธ์กฐํ๋ Signal์ด ๋ณ๊ฒฝ๋๋ฉด ์๋์ผ๋ก ๋น๋๊ธฐ ์์
์ ๋ค์ ํธ๋ฆฌ๊ฑฐํฉ๋๋ค.
use dioxus::prelude::*;
use serde::{Deserialize, Serialize};
#[derive(Deserialize, Serialize, Clone, Debug, PartialEq)]
struct GitHubRepo {
name: String,
description: Option<String>,
stargazers_count: u32,
html_url: String,
}
#[component]
fn RepoViewer() -> Element {
let mut org_name = use_signal(|| "DioxusLabs".to_string());
// org_name Signal์ด ๋ฐ๋ ๋๋ง๋ค ์๋์ผ๋ก ์ฌ์คํ๋๋ ๋ฆฌ์์ค
let repo_resource = use_resource(move || async move {
let org = org_name();
let url = format!("https://api.github.com/orgs/{org}/repos?per_page=5");
// reqwest๋ฅผ ์ด์ฉํ ๋น๋๊ธฐ GET ์์ฒญ (Wasm ์ง์)
reqwest::Client::new()
.get(&url)
.header("User-Agent", "Dioxus-App")
.send()
.await?
.json::<Vec<GitHubRepo>>()
.await
});
rsx! {
div { style: "padding: 1.5rem; max-width: 600px; margin: 0 auto; font-family: sans-serif;",
h2 { "๐ GitHub ๋ฆฌํฌ์งํ ๋ฆฌ ํ์๊ธฐ" }
div { style: "margin-bottom: 1rem;",
button {
style: "margin-right: 0.5rem; padding: 0.4rem 0.8rem; cursor: pointer;",
onclick: move |_| org_name.set("DioxusLabs".to_string()),
"DioxusLabs"
}
button {
style: "padding: 0.4rem 0.8rem; cursor: pointer;",
onclick: move |_| org_name.set("rust-lang".to_string()),
"rust-lang"
}
}
// ๋ฆฌ์์ค ์ํ ํจํด ๋งค์นญ
match &*repo_resource.read_unchecked() {
Some(Ok(repos)) => rsx! {
div {
for repo in repos {
div {
key: "{repo.name}",
style: "border: 1px solid #e2e8f0; border-radius: 6px; padding: 0.8rem; margin-bottom: 0.6rem;",
a { href: "{repo.html_url}", target: "_blank", style: "font-weight: bold; color: #2563eb;", "{repo.name}" }
span { style: "float: right; color: #eab308;", "โ
{repo.stargazers_count}" }
p { style: "color: #64748b; font-size: 0.9rem; margin: 0.4rem 0 0;", "{repo.description.as_deref().unwrap_or(\"์ค๋ช
์์\")}" }
}
}
}
},
Some(Err(err)) => rsx! {
div { style: "color: #ef4444; padding: 1rem; background: #fee2e2; border-radius: 6px;",
"โ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์ค๋ ์ค ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค: {err}"
}
},
None => rsx! {
div { style: "color: #64748b; text-align: center; padding: 2rem;",
"โณ ๋ฆฌํฌ์งํ ๋ฆฌ ๋ชฉ๋ก์ ๋ถ๋ฌ์ค๋ ์ค..."
}
}
}
}
}
}
๐ฆ 2. Dioxus Router: ํ์ ์์ ๋ผ์ฐํ
Dioxus์ ๋ผ์ฐํฐ๋ ์ปดํ์ผ ํ์์ ๋ชจ๋ ๊ฒฝ๋ก์ ์ ํจ์ฑ์ ๊ฒ์ฆํ๋ Rust enum ๊ธฐ๋ฐ ์์คํ
์
๋๋ค. ์๋ชป๋ ๊ฒฝ๋ก๋ก์ ์ด๋์ ์ปดํ์ผ ํ์์ ์ฆ์ ์๋ฌ๋ก ๊ฒ์ถ๋ฉ๋๋ค.
use dioxus::prelude::*;
// 1. ๋ผ์ฐํธ ์ด๊ฑฐํ ์ ์
#[derive(Routable, Clone, PartialEq)]
#[rustfmt::skip]
enum Route {
#[layout(NavBar)]
#[route("/")]
Home {},
#[route("/about")]
About {},
#[route("/user/:id")]
UserProfile { id: String },
#[end_layout]
#[route("/:..route")]
PageNotFound { route: Vec<String> },
}
// 2. ์ ์ฒด ๋ ์ด์์ (๋ค๋น๊ฒ์ด์
๋ฐ ํฌํจ)
#[component]
fn NavBar() -> Element {
rsx! {
nav { style: "display: flex; gap: 1rem; padding: 1rem; background: #1e293b; color: white;",
Link { to: Route::Home {}, "ํ" }
Link { to: Route::About {}, "์๊ฐ" }
Link { to: Route::UserProfile { id: "ferris".into() }, "๋ด ํ๋กํ" }
}
main { style: "padding: 1.5rem;",
Outlet::<Route> {} // ํ์ฌ ๋ผ์ฐํธ ์ปดํฌ๋ํธ๊ฐ ๋ ๋๋ง๋๋ ์ฌ๋กฏ
}
}
}
#[component]
fn Home() -> Element {
rsx! { h2 { "๐ ํ ํ๋ฉด์
๋๋ค" } }
}
#[component]
fn About() -> Element {
rsx! { h2 { "โน๏ธ Dioxus ์๊ฐ ํ์ด์ง์
๋๋ค" } }
}
#[component]
fn UserProfile(id: String) -> Element {
let nav = use_navigator();
rsx! {
div {
h2 { "๐ค ์ฌ์ฉ์ ํ๋กํ: {id}" }
button {
onclick: move |_| nav.push(Route::Home {}),
"ํ์ผ๋ก ํ๋ก๊ทธ๋๋ฐ ๋ฐฉ์ ์ด๋"
}
}
}
}
#[component]
fn PageNotFound(route: Vec<String>) -> Element {
rsx! {
div { style: "color: red;",
h2 { "404 - ํ์ด์ง๋ฅผ ์ฐพ์ ์ ์์ต๋๋ค" }
p { "์์ฒญ ๊ฒฝ๋ก: /{route.join(\"/\")}" }
}
}
}
// 3. ์ฑ ์ง์
์ ์์ ๋ผ์ฐํฐ ๋ง์ดํธ
#[component]
fn App() -> Element {
rsx! {
Router::<Route> {}
}
}
โก 3. Fullstack & #[server] ์๋ฒ ํจ์ (Zero-Boilerplate RPC)
Dioxus Fullstack์์๋ ํด๋ผ์ด์ธํธ ์ฝ๋์ ์๋ฒ ์ฝ๋๋ฅผ ๋์ผํ ํ์ผ์ ์์ฑํ ์ ์์ต๋๋ค. #[server] ์ด๋
ธํ
์ด์
์ด ๋ถ์ ํจ์๋ ์๋ฒ์์๋ง ์คํ๋๋ฉฐ, ํด๋ผ์ด์ธํธ๊ฐ ํธ์ถํ ๊ฒฝ์ฐ ์๋์ผ๋ก HTTP ์๋ํฌ์ธํธ ์ง๋ ฌํ ๋ฐ ์ญ์ง๋ ฌํ๊ฐ ์ํ๋ฉ๋๋ค.
use dioxus::prelude::*;
// ์๋ฒ์์๋ง ์คํ๋๋ ํจ์ (๋ฐ์ดํฐ๋ฒ ์ด์ค ์กฐํ, ๋น๋ฐ ํค ์ ๊ทผ ๋ฑ)
#[server]
async fn query_database_user(user_id: u32) -> Result<String, ServerFnError> {
// ์ด ๋ธ๋ก์ ๋ฐฑ์๋ ์๋ฒ(Axum)์์๋ง ๋์ํฉ๋๋ค.
println!("์๋ฒ DB ์ฟผ๋ฆฌ ์คํ: User ID {}", user_id);
// ๋ชจ์ DB ์กฐํ ์๋ต
Ok(format!("๋ฐ์ดํฐ๋ฒ ์ด์ค ์ ์ #{} (๋ณด์ ์ธ์ฆ ์๋ฃ)", user_id))
}
#[component]
fn ServerFnDemo() -> Element {
let mut server_result = use_signal(String::new);
rsx! {
div { style: "padding: 1rem; border: 1px dashed #6366f1; border-radius: 8px;",
h3 { "โก Fullstack Server Functions ์์ " }
button {
style: "background: #6366f1; color: white; padding: 0.5rem 1rem; border: none; border-radius: 4px; cursor: pointer;",
onclick: move |_| async move {
// ํด๋ผ์ด์ธํธ์์ ์ผ๋ฐ ๋น๋๊ธฐ ํจ์์ฒ๋ผ ์๋ฒ ํจ์๋ฅผ ์ง์ ํธ์ถ!
match query_database_user(42).await {
Ok(data) => server_result.set(data),
Err(err) => server_result.set(format!("์๋ฒ ์ค๋ฅ: {err}")),
}
},
"์๋ฒ์ ๋ณด์ ์ฟผ๋ฆฌ ์์ฒญ (RPC)"
}
if !server_result().is_empty() {
p { style: "color: #10b981; font-weight: bold; margin-top: 1rem;",
"์๋ฒ ์๋ต ๊ฒฐ๊ณผ: {server_result}"
}
}
}
}
}
๐ 4. ์ ์ญ ์ํ ๊ด๋ฆฌ (provide_context & use_context)
Props ๋๋ฆด๋ง ์์ด ํธ๋ฆฌ ๊น์ ๊ณณ์ ์์ ์ปดํฌ๋ํธ๋ค์๊ฒ ์ํ๋ ํ ๋ง๋ฅผ ๊ณต์ ํ ๋ Context API๋ฅผ ํ์ฉํฉ๋๋ค.
use dioxus::prelude::*;
#[derive(Clone, Copy, PartialEq)]
enum Theme {
Light,
Dark,
}
#[component]
fn RootApp() -> Element {
// 1. ๋ฃจํธ ์ปดํฌ๋ํธ์์ Signal ์ปจํ
์คํธ ์ฃผ์
let theme = use_signal(|| Theme::Dark);
provide_context(theme);
rsx! {
div {
class: if theme() == Theme::Dark { "dark-mode" } else { "light-mode" },
ChildComponent {}
}
}
}
#[component]
fn ChildComponent() -> Element {
// 2. ์์์ ๊น์ ์์ ์ปดํฌ๋ํธ์์ ์ปจํ
์คํธ ๊บผ๋ด๊ธฐ
let mut theme = use_context::<Signal<Theme>>();
rsx! {
button {
onclick: move |_| {
let next = if theme() == Theme::Dark { Theme::Light } else { Theme::Dark };
theme.set(next);
},
"ํ
๋ง ์ ํ (ํ์ฌ: {if theme() == Theme::Dark { \"๋คํฌ ๋ชจ๋\" } else { \"๋ผ์ดํธ ๋ชจ๋\" }})"
}
}
}
6. ํฌ๋ก์คํ๋ซํผ ๋น๋ ๋ฐ ์ต์ ํ (Cross-Platform Deployment)
Dioxus๋ dx CLI๋ฅผ ํตํด ๋จ์ผ ์์ค์ฝ๋๋ก๋ถํฐ Web, Desktop, Mobile ๋ค์ดํฐ๋ธ ๋ฐ์ด๋๋ฆฌ๋ฅผ ์ฆ์ ํจํค์งํ ์ ์์ต๋๋ค.
๐ป 1. ํ๋ซํผ๋ณ ์คํ ๋ฐ ๋น๋ ๋ช ๋ น์ด
# -------------------------------------------------------------
# 1. Web ํ๊น (WebAssembly)
# -------------------------------------------------------------
# ๋ก์ปฌ ๊ฐ๋ฐ ์๋ฒ ๊ตฌ๋ (ํฌํธ 8080)
dx serve --platform web
# ๋ฐฐํฌ์ฉ ํ๋ก๋์
Wasm ๋น๋ (dist/ ๋๋ ํ ๋ฆฌ์ ์ ์ ํ์ผ ์์ฑ)
dx build --platform web --release
# -------------------------------------------------------------
# 2. Desktop ํ๊น (Windows, macOS, Linux)
# -------------------------------------------------------------
# ๋ฐ์คํฌํฑ ๋ค์ดํฐ๋ธ ์ฐฝ์ผ๋ก ์คํ
dx serve --platform desktop
# OS๋ณ ๋ค์ดํฐ๋ธ ์ธ์คํจ๋ฌ ๋ฒ๋ค ํจํค์ง (.msi, .dmg, .deb, .AppImage)
dx bundle --platform desktop --release
# -------------------------------------------------------------
# 3. Mobile ํ๊น (Android / iOS)
# -------------------------------------------------------------
# Android ์๋ฎฌ๋ ์ดํฐ ๋๋ ์ฐ๊ฒฐ๋ ๋๋ฐ์ด์ค๋ก ์คํ
dx serve --platform android
# iOS ์๋ฎฌ๋ ์ดํฐ๋ก ์คํ (macOS ์ ์ฉ)
dx serve --platform ios
๐ 2. WebAssembly ํ๋ก๋์
์ต์ ํ (Cargo.toml)
Web ํ๊น ๋ฐฐํฌ ์ Wasm ํ์ผ ํฌ๊ธฐ๋ฅผ ์๋ฐฑ KB ๋จ์๋ก ๊ทน๋จ์ ์ผ๋ก ๋ค์ด์ดํธํ๊ธฐ ์ํด ๋ค์ ํ๋กํ์ผ ์ค์ ์ ์ ์ฉํฉ๋๋ค:
# Cargo.toml ํ๋จ์ ์ถ๊ฐ
[profile.release]
opt-level = "z" # ํฌ๊ธฐ(Size) ์ต์ ํ ์ฐ์ ('s' ๋๋ 'z')
lto = true # ๋งํฌ ํ์ ์ต์ ํ ํ์ฑํ (๋ถํ์ํ ์ฝ๋ ์์ ์ ๊ฑฐ)
codegen-units = 1 # ๋จ์ผ ์ฝ๋์ ์ ๋์ผ๋ก ์ต์ ํ ๊ทน๋ํ
panic = "abort" # ํจ๋ ์ธ์์ธ๋ฉ ๋ฉ์ปค๋์ฆ์ ์ ๊ฑฐํ์ฌ ๋ฐ์ด๋๋ฆฌ ์ถ์
strip = true # ๋๋ฒ๊ทธ ์ฌ๋ณผ ์์ ์คํธ๋ฆฝ
dx build --release ์คํ ์ ๊ธฐ๋ณธ ๋ด์ฅ๋ wasm-opt๊ฐ ์๋์ผ๋ก ๋ฐ์ด๋๋ฆฌ๋ฅผ ํ ๋ฒ ๋ ์์ถํ์ฌ ๋ธ๋ผ์ฐ์ ๋ก๋ฉ ์๋๋ฅผ ๋น์ฝ์ ์ผ๋ก ํฅ์์ํต๋๋ค.
7. ์ค๋ฌด ์ฃผ์์ฌํญ, ์์ฃผ ๊ฒช๋ ํจ์ & ํธ๋ฌ๋ธ์ํ (Troubleshooting)
โ ๏ธ 1. Dioxus 0.5์์ 0.6์ผ๋ก ๋ง์ด๊ทธ๋ ์ด์ ์ ์ฃผ์์ฌํญ
| ๊ธฐ์กด ๋ฐฉ์ (0.5 ์ดํ) | 0.6 ๊ถ์ฅ ๋ฐฉ์ | ๋ณ๊ฒฝ ์ฌ์ ๋ฐ ์ฃผ์์ |
|---|---|---|
| `use_state(cx, | 0)` | |
count.get() | count() ๋๋ count.read() | ํจ์ ํธ์ถ ์คํ์ผ์ ์ง๊ด์ ์ธ ์ฝ๊ธฐ ๋ฌธ๋ฒ ์ง์ |
count.set(count.get() + 1) | count += 1 | ๊ธฐ๋ณธ ํ์
์ ๋ํด AddAssign ๋ฑ ์ฐ์ฐ์ ์ค๋ฒ๋ก๋ฉ ์ง์ ์ง์ |
render(rsx! { ... }) | rsx! { ... } | render() ๋ํผ ๋ถํ์. ๋งคํฌ๋ก ๊ฒฐ๊ณผ๋ฅผ ๊ทธ๋๋ก Element๋ก ๋ฐํ |
#[inline_props] | ์๋ ์ฒ๋ฆฌ | #[component] ๋งคํฌ๋ก๊ฐ ๋ชจ๋ ์ธ์๋ฅผ ์๋์ผ๋ก ๊ฐ์ง |
โ ๏ธ 2. Rust ์์ ๊ถ๊ณผ ๋น๋๊ธฐ ํด๋ก์ (move || async move)
์ด๋ฒคํธ ํธ๋ค๋ฌ๋ use_resource ๋ด๋ถ์์ ๋น๋๊ธฐ ์์
์ ์ํํ ๋ ๋ณ์์ ์์ ๊ถ ์ด๋(move)์ ์ฃผ์ํด์ผ ํฉ๋๋ค:
// โ ํํ ์ค์: ์ธ๋ถ์ Signal์ ํด๋ก์ ๋ก ์ฌ๋ฌ ๋ฒ ์ด๋์ํค๋ ค ํ ๋ ์์ ๊ถ ์๋ฌ ๋ฐ์
let mut query = use_signal(String::new);
rsx! {
button {
// Rust Borrow Checker ์๋ฌ ๊ฐ๋ฅ์ฑ
onclick: move |_| {
println!("ํ์ฌ ๊ฒ์์ด: {}", query());
}
}
}
// โ
์ฌ๋ฐ๋ฅธ ํจํด: Dioxus 0.6์ Signal์ ๋ด๋ถ์ ์ผ๋ก `Copy` ํธ๋ ์ดํธ๋ฅผ ๊ตฌํํ๋ฏ๋ก
// ์๋ฌด ์ ์ฝ ์์ด ์ฌ๋ฌ ํด๋ก์ ๋ด๋ถ๋ก ์์ ๋กญ๊ฒ `move` ์ ๋ฌ์ด ๊ฐ๋ฅํฉ๋๋ค!
let mut query = use_signal(String::new);
rsx! {
button {
onclick: move |_| {
// Signal์ Copy์ด๋ฏ๋ก ๋ฌธ์ ์์ด ์์ ํ๊ฒ ์ฝ๊ธฐ/์ฐ๊ธฐ ๊ฐ๋ฅ
query.set("์ ๊ฒ์์ด".to_string());
}
}
}
[!TIP] ๋์ฉ๋ ๊ฐ์ฒด๋ ๋ณต์ฌ ๋ถ๊ฐ๋ฅํ ๋ฐ์ดํฐ์ ๊ฒฝ์ฐ:
Signal๋ด๋ถ์ ๋์ฉ๋Vec์ด๋ ๊ตฌ์กฐ์ฒด๋ฅผ ๋ณด๊ดํ ๋๋data.read()๋ก ๋ถํ์ํ ํด๋ก ์ ํผํ๊ณ , ๋ณ๊ฒฝ ์์๋งdata.write()๊ฐ๋ณ ๋์ฌ๋ฅผ ์งง๊ฒ ์ ์งํ์ธ์. ๋์ฌ(read/write)๋ฅผ ๋ค๊ณ ์๋ ์ํ์์ ๋น๋๊ธฐ.await๋ฅผ ํธ์ถํ๋ฉด ๋ฐ๋๋ฝ์ด ๋ฐ์ํ ์ ์์ต๋๋ค.
โ ๏ธ 3. ๋ฆฌ๋ ์ค ํ๊ฒฝ ๋ฐ์คํฌํฑ ์คํ ์ WebKitGTK ์ค๋ฅ
Ubuntu ๋ฑ Linux ํ๊ฒฝ์์ dx serve --platform desktop ์คํ ์ GLib-GIO-ERROR: No GSettings schemas are installed ๋๋ WebKitGTK ๋๋ฝ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ ๊ฒฝ์ฐ:
# WebKitGTK 4.1 ๋ฐํ์ ๋ฐ ์คํค๋ง ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ ์ค์น
sudo apt install -y libwebkit2gtk-4.1-0 gsettings-desktop-schemas
# Wayland ํ๊ฒฝ์์ ์น๋ทฐ ์ถฉ๋ ์ X11 ํธํ ๋ชจ๋๋ก ๊ฐ์ ์คํ
GDK_BACKEND=x11 dx serve --platform desktop
8. ํ๋ก๋์ ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ (Production Checklist)
- Wasm ์ต์ ํ ํ์ธ:
Cargo.toml์[profile.release]์ต์ ํ ํ๋๊ทธ(opt-level = "z",lto = true,strip = true)๋ฅผ ์ ์ฉํ๋๊ฐ? - ์ ์ ํ์ผ ์์ถ: ์น ์๋ฒ(Nginx, Cloudflare Pages ๋ฑ)์์
.wasm๋ฐ.jsํ์ผ์ ๋ํด Brotli ๋๋ Gzip ์์ถ์ ํ์ฑํํ๋๊ฐ? - MIME ํ์
์ค์ : ์๋ฒ๊ฐ
.wasmํ์ผ์ ๋ํดapplication/wasmํค๋๋ฅผ ์ ํํ ๋ฐํํ๋์ง ๊ฒ์ฆํ๋๊ฐ? - ๋ผ์ฐํฐ Fallback ์ค์ : SPA(Single Page Application) ๋ฐฐํฌ ์ 404 ์์ฒญ์
index.html๋ก ์ฌ์์ฑ(Rewrite)ํ๋๋ก ๋ผ์ฐํ ๊ท์น์ ๊ตฌ์ฑํ๋๊ฐ? - ๋ฐ์คํฌํฑ ๋ณด์ ๊ถํ: ๋ฐ์คํฌํฑ ๋ฒ๋ค๋ง ์ ํ์ผ ์์คํ , ํด๋ฆฝ๋ณด๋, ๋คํธ์ํฌ ์ ๊ทผ ๊ถํ์ด ์ต์ ๊ถํ ์์น(Principle of Least Privilege)์ ๋ถํฉํ๋๊ฐ?
-
dx bundle์๋ช : macOS(Gatekeeper ๊ณต์ฆ ์ฝ๋ ์๋ช ) ๋ฐ Windows(์ธ์ฆ์ ์๋ช ) ํจํค์ง์ ์๋ฃํ๋๊ฐ?
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์