← ClaudeAtlas

calling-rust-from-tauri-frontendlisted

Call Rust backend functions from the Tauri v2 frontend with invoke and #[tauri::command]. USE WHEN defining commands, passing arguments or return values across IPC, handling command errors, or wiring async frontend-to-Rust calls.
Sheshiyer/skill-clusters · ★ 0 · Web & Frontend · score 69
Install: claude install-skill Sheshiyer/skill-clusters
# Calling Rust from Tauri Frontend This skill covers how to call Rust backend functions from your Tauri v2 frontend using the command system and invoke function. ## Overview Tauri provides two IPC mechanisms: - **Commands** (recommended): Type-safe function calls with serialized arguments/return values - **Events**: Dynamic, one-way communication (not covered here) ## Basic Commands ### Defining a Command in Rust Use the `#[tauri::command]` attribute macro: ```rust // src-tauri/src/lib.rs #[tauri::command] fn greet(name: String) -> String { format!("Hello, {}!", name) } ``` ### Registering Commands Commands must be registered with the invoke handler: ```rust #[cfg_attr(mobile, tauri::mobile_entry_point)] pub fn run() { tauri::Builder::default() .invoke_handler(tauri::generate_handler![greet, login, fetch_data]) .run(tauri::generate_context!()) .expect("error while running tauri application") } ``` ### Invoking from JavaScript/TypeScript ```typescript import { invoke } from '@tauri-apps/api/core'; const greeting = await invoke('greet', { name: 'World' }); console.log(greeting); // "Hello, World!" ``` Or with the global Tauri object (when `app.withGlobalTauri` is enabled): ```javascript const { invoke } = window.__TAURI__.core; const greeting = await invoke('greet', { name: 'World' }); ``` ## Passing Arguments ### Argument Naming Convention By default, Rust snake_case arguments map to JavaScript camelCase: ```rust #[tauri::comm