如何使用Rocket.rs在URL中使用日期?

问题描述 投票:0回答:1

你如何改变Rocket网站上的例子来取一个日期而不是一个年龄/ u8

网站上的例子:

#![feature(proc_macro_hygiene, decl_macro)]

#[macro_use] extern crate rocket;

#[get("/hello/<name>/<age>")]
fn hello(name: String, age: u8) -> String {
    format!("Hello, {} year old named {}!", age, name)
}

fn main() {
    rocket::ignite().mount("/", routes![hello]).launch();
}

我想要或多或少相同的输出(你好,58岁,名叫约翰!),但有类似的东西

#[get("/hello/<name>/<birthdate>")]

而不是那个

#[get("/hello/<name>/<age>")]

我认为正确的结构是chrono::DateTime并且不知何故rocket::request::FromParam参与其中但我从​​那里有点失落。

rust rust-rocket
1个回答
0
投票

NaiveDate可以表示为“自1月1日以来的天数”,类型为i32。有方法self.num_days_from_ce()from_num_days_from_ce()。我相信这是最方便的方式。 Documentation

© www.soinside.com 2019 - 2024. All rights reserved.