我正在尝试创建一个向客户端返回字符串的 Rocket 路由,但我无法让它工作。到目前为止,这就是我所拥有的:
#![feature(plugin)]
#![plugin(rocket_codegen)]
#[macro_use]
extern crate serde_derive;
extern crate toml;
extern crate rocket;
mod utilities;
mod pipeline_config;
mod assets;
use std::path::PathBuf;
#[get("/resources/<path..>")]
fn get_resource(path: PathBuf) -> Result<String, ()> {
if let Some(page_path) = path.to_str() {
match assets::get(&format!("{}/{}", "resources", page_path)) {
Ok(page) => Ok(utf8_to_string(page)),
Err(e) => Err(()),
}
}
Err(())
}
fn main() {
let rocket_obj = rocket::ignite();
rocket_obj.mount("/", routes![get_resource]).launch();
}
pub fn utf8_to_string(bytes: &[u8]) -> String {
let vector: Vec<u8> = Vec::from(bytes);
String::from_utf8(vector).unwrap()
}
看起来它应该可以工作,但它给了我一个错误expected (), found enum std::result::Result
:
error[E0308]: mismatched types
--> src/main.rs:18:9
|
18 | / match assets::get(&format!("{}/{}", "resources", page_path)) {
19 | | Ok(page) => Ok(utf8_to_string(page)),
20 | | Err(e) => Err(()),
21 | | }
| | ^- help: try adding a semicolon: `;`
| |_________|
| expected (), found enum `std::result::Result`
|
= note: expected type `()`
found type `std::result::Result<std::string::String, ()>`
这对我来说毫无意义,因为我要返回 aResult
和 a String
。