无法访问 Iron 中的参数,因为不满足特征界限 plugin::Plugin<iron::Request>

Cannot access parameters in Iron because the trait bound plugin::Plugin<iron::Request> is not satisfied

我正在探索 Iron Web 框架的功能。据我所知,铁芯没有 API 可以处理 HTTP 参数,所以我尝试使用 params crate。

error: the trait bound `params::Params: plugin::Plugin<iron::Request<'_, '_>>` is not satisfied [E0277]
    let map = req.get_ref::<Params>().unwrap();
                  ^~~~~~~
help: run `rustc --explain E0277` to see a detailed explanation

我还没有找到这个错误的踪迹,也不知道如何修复它。

extern crate iron;
extern crate params;

use iron::prelude::*;
use iron::status;
use params::*; //{self, Params, Value};

fn handle_user(req: &mut Request) -> IronResult<Response> {
    use params::{Params, Value};

    let map = req.get_ref::<Params>().unwrap();

    match map.find(&["user", "name"]) {
        Some(&Value::String(ref name)) if name == "Marie" => {
            Ok(Response::with((iron::status::Ok, "Welcome back, Marie!")))
        },
        _ => Ok(Response::with(iron::status::NotFound)),
    }
}

fn main() {   
    Iron::new(handle_user).http("localhost:2330").unwrap();
}

库版本

iron = "0.4.0"
params = "0.2.2"

params 0.2.2 crate depends on iron ^0.3,所以需要将铁依赖版本更改为0.3.

使用此类插件箱时,必须确保版本完全匹配。有时可能还需要 cargo update


在 Rust 中,从同一个 crate 的多个版本中获取的相同结构或特征被视为完全不同。它通常会导致类似“Pixel expected, but found Pixel” 的错误,或者像您的情况那样缺少特征实现。