1.搭建rust服务器步骤:http://ppsbbs.tech/thread-458.htm
2.创建服务器工程
cargo new rust-web-server --bin
cd rust-web-server
cargo vendor
3.编辑Cargo.toml
[package]
name = "rust-web-server"
version = "0.1.0"
authors = ["xingyun86"]
edition = "2019"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
rocket = "0.4.2"
此处注意,rocket新版本为0.4.X。相比0.3做了不少的优化和修改,详情大家可以自行去官网查看https://rocket.rs/。
4.编写代码main.rs
#![feature(proc_macro_hygiene, decl_macro)]
#[macro_use] extern crate rocket;
#[get("/<name>/<age>")]
fn hello(name: String, age: u8) -> String {
format!("Hello, {} year old named {}!", age, name)
}
fn main() {
rocket::ignite().mount("/hello", routes![hello]).launch();
}
5.编译运行
cargo build
.\target\debug\rust-web-server.exe
或
cargo run
6.效果预览
D:\Work\Workspace\RustProjects\rust-web-server>.\target\debug\rust-web-server.exe
Configured for development.
=> address: localhost
=> port: 8000
=> log: normal
=> workers: 16
=> secret key: generated
=> limits: forms = 32KiB
=> keep-alive: 5s
=> tls: disabled
Mounting /hello:
=> GET /hello/<name>/<age> (hello)
Rocket has launched from http://localhost:8000