Steward
分享是一種喜悅、更是一種幸福
程式語言 - Rust - Web Server
參考資訊:
https://www.rust-lang.org/learn/get-started
https://www.readfog.com/a/1673773161454342144
http://bzz.wallizard.com:8081/share/books/RUST/Programming%20Rust%202nd%20Edition.pdf
產生樣板
$ cargo new hello $ cd hello
Cargo.toml
[package] name = "hello" version = "0.1.0" edition = "2021" [dependencies] axum = "0" tokio = { version = "1", default-features = false, features = ["macros", "rt-multi-thread"] }
src/main.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | use std :: net :: SocketAddr ; use axum ::{ response :: Html , routing :: get , Router }; #[tokio::main] async fn main () { let app = Router :: new (). route ( "/" , get ( handler )); let addr = SocketAddr :: from (([127, 0, 0, 1], 3000)); axum :: Server :: bind (&addr) . serve (app. into_make_service ()) . await . unwrap (); } async fn handler () -> Html <&' static str > { Html ( "<h1>Hello, world!</h1>" ) } |
執行
$ cargo run
開啟網頁並且輸入http://127.0.0.1:3000