意外的T vs& T作为Rust中的类型参数

时间:2016-05-16 16:15:55

标签: rust

我对我写的s进行了通用化。当前签名是

t

我在特定的函数中使用它,并且对其他函数的调用无法编译。

首先,使用:

Graph

#[derive(Debug)] pub struct Graph<T: Clone + Hash + Eq> { nodes: HashMap<T, Node<T>>, edges: HashMap<T, Vec<Edge<T>>> } #[derive(PartialEq, Debug)] pub struct Node<T: Clone + Hash + Eq> { pub id: T, pub x: f64, pub y: f64 } #[derive(PartialEq, Debug)] pub struct Edge<T: Clone + Hash + Eq> { pub id: T, pub from_id: T, pub to_id: T, pub weight: i64 }

的签名
fn reducer<T>(graph: Graph<T>, untested_nodes: HashSet<T>, mut results: Vec<HashSet<T>>) -> Graph<T>
   where T: Clone + Hash + Eq {
    match untested_nodes.iter().next() {
        None => {
            collapsed_graph(&graph, &results)
        }
        Some(root) => {
            let connected_nodes = explore_from(&root, &graph);
            let difference = untested_nodes.difference(&connected_nodes)
                                           .cloned()
                                           .collect();
            results.push(connected_nodes);
            reducer(graph,
                    difference,
                    results
                    )
        }
    }
}

编译错误:

explore_from

完整代码:https://github.com/shterrett/efficient_route_planning/blob/generic-graph/src/connected_component.rs#L19

据我所知,一切都应该是fn explore_from<T: Clone + Hash + Eq>(root: &T, graph: &Graph<T>) -> HashSet<T> { ,而不是 Compiling efficient_route_planning v0.1.0 (file:///Users/stuart/coding/efficient_route_planning) src/connected_component.rs:19:55: 19:61 error: mismatched types: expected `&weighted_graph::Graph<&T>`, found `&weighted_graph::Graph<T>` (expected &-ptr, found type parameter) [E0308] src/connected_component.rs:19 let connected_nodes = explore_from(&root, &graph); ^~~~~~ src/connected_component.rs:19:55: 19:61 help: run `rustc --explain E0308` to see a detailed explanation src/connected_component.rs:20:56: 20:72 error: mismatched types: expected `&std::collections::hash::set::HashSet<T>`, found `&std::collections::hash::set::HashSet<&T>` (expected type parameter, found &-ptr) [E0308] src/connected_component.rs:20 let difference = untested_nodes.difference(&connected_nodes) ^~~~~~~~~~~~~~~~ src/connected_component.rs:20:56: 20:72 help: run `rustc --explain E0308` to see a detailed explanation src/connected_component.rs:23:26: 23:41 error: mismatched types: expected `std::collections::hash::set::HashSet<T>`, found `std::collections::hash::set::HashSet<&T>` (expected type parameter, found &-ptr) [E0308] src/connected_component.rs:23 results.push(connected_nodes); ^~~~~~~~~~~~~~~ src/connected_component.rs:23:26: 23:41 help: run `rustc --explain E0308` to see a detailed explanation error: aborting due to 3 previous errors Could not compile `efficient_route_planning`. 。我不确定错误在哪里。

版本:

&Graph<T>

1 个答案:

答案 0 :(得分:4)

问题似乎在这里

let connected_nodes = explore_from(&root, &graph);

untested_nodes.iter().next()会返回Option<&T>,因此匹配Some(root)会生成root: &T。这意味着&root&&T,而T中推断的explore_from&TT的{​​{1}})。我希望从reducer删除引用可以解决此问题:

root