A simple login form built with Dioxus — a Rust-based full-stack UI framework — demonstrating modular Rust code, asynchronous form handling, and type-safe client-server communication.
src/
├── main.rs # Entrypoint setting up routing, styling, and app launch
├── auth.rs # Authentication logic and server-side login function
└── home.rs # Home page component with the login form
assets/
└── style.css # CSS styles for the app
This example covers:
- Modular Rust project structure for maintainability
#[server]async decorated login function with automatic RPC support- Login form handling with uncontrolled components for performance
- Generic form data extraction function using Rust iterators
- Asynchronous submission preventing page reloads
- Type-safe serialization/deserialization with Serde for client-server comms
- Logging login attempts and results in the browser console
LoginFormstruct withusernameandpassword- Constructor
newfor ergonomic instance creation #[server]asyncloginfunction validating credentials and returning results
Homecomponent rendering the login formonsubmithandler preventing default reload and asynchronously calling login- Uses generic
extract_textto parse form input values efficiently - Logs success or error messages in browser console
- App entry point setting up routing and CSS
- Loads global stylesheet via Dioxus asset system
- Launches the app using
dioxus::launch
-
Why use uncontrolled components?
They efficiently handle simple form input without extra re-renders or memory overhead.
-
Explain the
#[server]macro.It auto-generates client-server RPC code with seamless serialization and async support.
-
Why generic
extract_text?To accept any iterator and avoid unnecessary allocations, following idiomatic Rust patterns.
-
Benefits of modular
auth.rsandhome.rs?Clear separation of concerns improves maintainability and clarity.
-
Why is serialization important?
It ensures robust, type-safe communication over the network in full-stack Rust apps.
- Install Rust and Cargo
- Clone the repository
- Run
cargo runordx serveif you have dioxus-cli installed - Open your browser to
http://localhost:8080(default) to see the app
Made with ❤️ using Rust and Dioxus.
- Official Dioxus: https://dioxuslabs.com/
- Serde Serialization: https://serde.rs/
- Rust async book: https://rust-lang.github.io/async-book/