Rocket
初始化
-
使用 Rust 最新特性:
rustup default stable -
创建 Rust 项目:
cargo new hello-rocket --bin cd hello-rocket -
在
Cargo.toml中添加rocket依赖rocket = { version = "=0.5.0-rc.3", features = ["json"] } -
在
main.rs中添加下面的代码use rocket::{get, launch, routes}; #[get("/")] fn index() -> &'static str { "Hello world!" } #[launch] fn rocket() -> _ { rocket::build().mount("/", routes![index]) } // 或者 #[rocket::main] async fn main() -> Result<(), Box<dyn std::error::Error>> { rocket::build().mount("/", routes![index]).launch().await?; Ok(()) } -
执行
cargo run -
浏览器访问:
http://127.0.0.1:8000/, 可以看到Hello world!