这是一个(有些人为的)示例来说明我想做的事情
pub trait Node: Eq + Hash {
type Edge: Edge;
fn get_in_edges(&self) -> Vec<&Self::Edge>;
fn get_out_edges(&self) -> Vec<&Self::Edge>;
}
pub trait Edge {
type Node: Node;
fn get_src(&self) -> &Self::Node;
fn get_dst(&self) -> &Self::Node;
}
pub trait Graph {
type Node: Node;
type Edge: Edge;
fn get_nodes(&self) -> Vec<Self::Node>;
}
pub fn dfs<G: Graph>(root: &G::Node) {
let mut stack = VecDeque::new();
let mut visited = HashSet::new();
stack.push_front(root);
while let Some(n) = stack.pop_front() {
if visited.contains(n) {
continue
}
visited.insert(n);
for e in n.get_out_edges() {
stack.push_front(e.get_dst());
}
}
}
有没有一种方法可以表达必须与 相同类型且必须与 相同类型的Graph
特征?Graph::Node
Graph::Edge::Node
Graph::Edge
Graph::Node::Edge
我记得读过一些关于一个特性(当时没有实现)的东西,它可以为这类事情提供更丰富的约束,但我不记得它的名字,也找不到它。