Loading...
Loading...
Compare original and translation side by side
Build cross-platform desktop and mobile apps with web frontends and Rust backends.
使用Web前端和Rust后端构建跨平台桌面与移动应用。
| Metric | Without Skill | With Skill |
|---|---|---|
| Setup Time | ~2 hours | ~30 min |
| Common Errors | 8+ | 0 |
| Token Usage | High (exploration) | Low (direct patterns) |
| 指标 | 未使用本技能 | 使用本技能 |
|---|---|---|
| 搭建时间 | ~2小时 | ~30分钟 |
| 常见错误 | 8+ | 0 |
| 令牌使用 | 高(探索式) | 低(直接匹配模式) |
generate_handler!generate_handler!// src-tauri/src/lib.rs
#[tauri::command]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
}
pub fn run() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}generate_handler![]// src-tauri/src/lib.rs
#[tauri::command]
fn greet(name: String) -> String {
format!("Hello, {}!", name)
}
pub fn run() {
tauri::Builder::default()
.invoke_handler(tauri::generate_handler![greet])
.run(tauri::generate_context!())
.expect("error while running tauri application");
}generate_handler![]import { invoke } from '@tauri-apps/api/core';
const greeting = await invoke<string>('greet', { name: 'World' });
console.log(greeting); // "Hello, World!"@tauri-apps/api/core@tauri-apps/api/tauriimport { invoke } from '@tauri-apps/api/core';
const greeting = await invoke<string>('greet', { name: 'World' });
console.log(greeting); // "Hello, World!"@tauri-apps/api/core@tauri-apps/api/tauri// src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"windows": ["main"],
"permissions": ["core:default"]
}// src-tauri/capabilities/default.json
{
"$schema": "../gen/schemas/desktop-schema.json",
"identifier": "default",
"windows": ["main"],
"permissions": ["core:default"]
}tauri::generate_handler![cmd1, cmd2, ...]Result<T, E>Mutex<T>lib.rstauri::generate_handler![cmd1, cmd2, ...]Result<T, E>Mutex<T>lib.rs&strapp.path()&strapp.path()#[tauri::command]
async fn bad(name: &str) -> String { // Compile error!
name.to_string()
}#[tauri::command]
async fn good(name: String) -> String {
name
}#[tauri::command]
async fn bad(name: &str) -> String { // 编译错误!
name.to_string()
}#[tauri::command]
async fn good(name: String) -> String {
name
}| Issue | Root Cause | Solution |
|---|---|---|
| "Command not found" | Missing from | Add command to handler macro |
| "Permission denied" | Missing capability | Add to |
| State panic on access | Type mismatch in | Use exact type from |
| White screen on launch | Frontend not building | Check |
| IPC timeout | Blocking async command | Remove blocking code or use spawn |
| Mobile build fails | Missing Rust targets | Run |
| 问题 | 根因 | 解决方案 |
|---|---|---|
| "Command not found"(命令未找到) | 未加入 | 将命令添加到处理宏中 |
| "Permission denied"(权限拒绝) | 缺少capability | 添加到 |
| 状态访问时panic | | 使用 |
| 启动时白屏 | 前端未构建完成 | 检查配置中的 |
| IPC超时 | 异步命令阻塞 | 移除阻塞代码或使用spawn |
| 移动构建失败 | 缺少Rust目标平台 | 运行 |
{
"$schema": "./gen/schemas/desktop-schema.json",
"productName": "my-app",
"version": "1.0.0",
"identifier": "com.example.myapp",
"build": {
"devUrl": "http://localhost:5173",
"frontendDist": "../dist",
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build"
},
"app": {
"windows": [{
"label": "main",
"title": "My App",
"width": 800,
"height": 600
}],
"security": {
"csp": "default-src 'self'; img-src 'self' data:",
"capabilities": ["default"]
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": ["icons/icon.icns", "icons/icon.ico", "icons/icon.png"]
}
}build.devUrlapp.security.capabilities{
"$schema": "./gen/schemas/desktop-schema.json",
"productName": "my-app",
"version": "1.0.0",
"identifier": "com.example.myapp",
"build": {
"devUrl": "http://localhost:5173",
"frontendDist": "../dist",
"beforeDevCommand": "npm run dev",
"beforeBuildCommand": "npm run build"
},
"app": {
"windows": [{
"label": "main",
"title": "My App",
"width": 800,
"height": 600
}],
"security": {
"csp": "default-src 'self'; img-src 'self' data:",
"capabilities": ["default"]
}
},
"bundle": {
"active": true,
"targets": "all",
"icon": ["icons/icon.icns", "icons/icon.ico", "icons/icon.png"]
}
}build.devUrlapp.security.capabilities[package]
name = "app"
version = "0.1.0"
edition = "2021"
[lib]
name = "app_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"[lib]crate-type[package]
name = "app"
version = "0.1.0"
edition = "2021"
[lib]
name = "app_lib"
crate-type = ["staticlib", "cdylib", "rlib"]
[build-dependencies]
tauri-build = { version = "2", features = [] }
[dependencies]
tauri = { version = "2", features = [] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"[lib]crate-typeuse thiserror::Error;
#[derive(Debug, Error)]
enum AppError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Not found: {0}")]
NotFound(String),
}
impl serde::Serialize for AppError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::ser::Serializer {
serializer.serialize_str(self.to_string().as_ref())
}
}
#[tauri::command]
fn risky_operation() -> Result<String, AppError> {
Ok("success".into())
}use thiserror::Error;
#[derive(Debug, Error)]
enum AppError {
#[error("IO error: {0}")]
Io(#[from] std::io::Error),
#[error("Not found: {0}")]
NotFound(String),
}
impl serde::Serialize for AppError {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: serde::ser::Serializer {
serializer.serialize_str(self.to_string().as_ref())
}
}
#[tauri::command]
fn risky_operation() -> Result<String, AppError> {
Ok("success".into())
}use std::sync::Mutex;
use tauri::State;
struct AppState {
counter: u32,
}
#[tauri::command]
fn increment(state: State<'_, Mutex<AppState>>) -> u32 {
let mut s = state.lock().unwrap();
s.counter += 1;
s.counter
}
// In builder:
tauri::Builder::default()
.manage(Mutex::new(AppState { counter: 0 }))use std::sync::Mutex;
use tauri::State;
struct AppState {
counter: u32,
}
#[tauri::command]
fn increment(state: State<'_, Mutex<AppState>>) -> u32 {
let mut s = state.lock().unwrap();
s.counter += 1;
s.counter
}
// 在构建器中配置:
tauri::Builder::default()
.manage(Mutex::new(AppState { counter: 0 }))use tauri::Emitter;
#[tauri::command]
fn start_task(app: tauri::AppHandle) {
std::thread::spawn(move || {
app.emit("task-progress", 50).unwrap();
app.emit("task-complete", "done").unwrap();
});
}import { listen } from '@tauri-apps/api/event';
const unlisten = await listen('task-progress', (e) => {
console.log('Progress:', e.payload);
});
// Call unlisten() when doneuse tauri::Emitter;
#[tauri::command]
fn start_task(app: tauri::AppHandle) {
std::thread::spawn(move || {
app.emit("task-progress", 50).unwrap();
app.emit("task-complete", "done").unwrap();
});
}import { listen } from '@tauri-apps/api/event';
const unlisten = await listen('task-progress', (e) => {
console.log('Progress:', e.payload);
});
// 完成后调用unlisten()use tauri::ipc::Channel;
#[derive(Clone, serde::Serialize)]
#[serde(tag = "event", content = "data")]
enum DownloadEvent {
Progress { percent: u32 },
Complete { path: String },
}
#[tauri::command]
async fn download(url: String, on_event: Channel<DownloadEvent>) {
for i in 0..=100 {
on_event.send(DownloadEvent::Progress { percent: i }).unwrap();
}
on_event.send(DownloadEvent::Complete { path: "/downloads/file".into() }).unwrap();
}import { invoke, Channel } from '@tauri-apps/api/core';
const channel = new Channel<DownloadEvent>();
channel.onmessage = (msg) => console.log(msg.event, msg.data);
await invoke('download', { url: 'https://...', onEvent: channel });use tauri::ipc::Channel;
#[derive(Clone, serde::Serialize)]
#[serde(tag = "event", content = "data")]
enum DownloadEvent {
Progress { percent: u32 },
Complete { path: String },
}
#[tauri::command]
async fn download(url: String, on_event: Channel<DownloadEvent>) {
for i in 0..=100 {
on_event.send(DownloadEvent::Progress { percent: i }).unwrap();
}
on_event.send(DownloadEvent::Complete { path: "/downloads/file".into() }).unwrap();
}import { invoke, Channel } from '@tauri-apps/api/core';
const channel = new Channel<DownloadEvent>();
channel.onmessage = (msg) => console.log(msg.event, msg.data);
await invoke('download', { url: 'https://...', onEvent: channel });references/capabilities-reference.mdipc-patterns.mdNote: For deep dives on specific topics, see the reference files above.
references/capabilities-reference.mdipc-patterns.md注意: 如需深入了解特定主题,请查阅上述参考文档。
| Package | Version | Purpose |
|---|---|---|
| ^2.0.0 | CLI tooling |
| ^2.0.0 | Frontend APIs |
| ^2.0.0 | Rust core |
| ^2.0.0 | Build scripts |
| 包 | 版本 | 用途 |
|---|---|---|
| ^2.0.0 | CLI工具链 |
| ^2.0.0 | 前端API |
| ^2.0.0 | Rust核心库 |
| ^2.0.0 | 构建脚本 |
| Package | Version | Purpose |
|---|---|---|
| ^2.0.0 | File system access |
| ^2.0.0 | Native dialogs |
| ^2.0.0 | Shell commands, open URLs |
| ^2.0.0 | HTTP client |
| ^2.0.0 | Key-value storage |
| 包 | 版本 | 用途 |
|---|---|---|
| ^2.0.0 | 文件系统访问 |
| ^2.0.0 | 原生对话框 |
| ^2.0.0 | Shell命令、URL打开 |
| ^2.0.0 | HTTP客户端 |
| ^2.0.0 | 键值存储 |
devUrlbeforeDevCommanddevUrlbeforeDevCommandinvoke()generate_handler![]invoke()generate_handler![]undefinedundefinedundefinedundefinednpx tauri infosrc-tauri/capabilities/default.jsoncore:defaultgenerate_handler![]lib.rsnpx tauri infosrc-tauri/capabilities/default.jsoncore:defaultgenerate_handler![]lib.rs