R
Rust布道师
4周前
Rust 在 Web 开发中的崛起
RustWebAssembly后端
Rust 在 Web 开发中的应用
探索 Rust 如何改变 Web 开发格局。
1. 为什么选择 Rust
2. Axum 框架
rust
use axum::{routing::get, Router};
async fn hello() -> &'static str {
"Hello, World!"
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/", get(hello));
axum::serve(listener, app).await.unwrap();
}
3. Leptos 全栈框架
rust
#[component]
fn Counter() -> impl IntoView {
let (count, set_count) = create_signal(0);
view! {
"Count: " {count}
} }
4. WebAssembly
rust
use wasm_bindgen::prelude::*;
#[wasm_bindgen]
pub fn fibonacci(n: u32) -> u32 {
match n {
0 | 1 => n,
_ => fibonacci(n - 1) + fibonacci(n - 2)
}
}
5. 性能对比
| 框架 | 请求/秒 | 延迟 |
| ------ | --------- | ------ |
| Axum | 450,000 | 0.2ms |
| Express | 45,000 | 2ms |
| Fastify | 80,000 | 1.2ms |
4.1k 阅读