Using Rust Crates in Tauri: regex, chrono & uuid | Lesson 73
Video: Using Rust Crates in Tauri: regex, chrono & uuid | Lesson 73 by Taught by Celeste AI - AI Coding Coach
Watch full page →Using Rust Crates in Tauri: regex, chrono & uuid
Enhance your Tauri applications by integrating popular Rust crates from crates.io. This guide demonstrates how to add and use the regex, chrono, and uuid crates for pattern matching, date/time formatting, and generating unique identifiers, respectively, while exposing their functionality as Tauri commands callable from a React frontend.
Code
use regex::Regex;
use chrono::Local;
use uuid::Uuid;
use tauri::command;
// Extract emails from input text using regex
#[command]
fn extract_emails(text: &str) -> Vec<String> {
let email_re = Regex::new(r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}").unwrap();
email_re.find_iter(text).map(|mat| mat.as_str().to_string()).collect()
}
// Get the current local date/time formatted as RFC 2822 string
#[command]
fn current_datetime() -> String {
let now = Local::now();
now.to_rfc2822()
}
// Generate a new UUID v4 string
#[command]
fn generate_uuid() -> String {
Uuid::new_v4().to_string()
}
// In your main.rs or lib.rs, register commands:
// fn main() {
// tauri::Builder::default()
// .invoke_handler(tauri::generate_handler![extract_emails, current_datetime, generate_uuid])
// .run(tauri::generate_context!())
// .expect("error while running tauri application");
// }
Key Points
- Use
cargo addto easily add crates likeregex,chrono, anduuidto your Tauri project dependencies. - The
regexcrate provides fast and safe pattern matching for extracting data such as emails from strings. chronoenables flexible and human-readable date/time formatting based on the current local time.uuidgenerates universally unique identifiers (UUID v4) useful for many application needs.- Expose Rust crate functionality as Tauri commands to seamlessly call them from your frontend framework like React.