1
此代碼:任何方式從結構中獲取關聯的類型?
use std::collections::HashMap;
struct MyNode;
struct MyEdge;
struct Graph<N, E> {
h: HashMap<N, Vec<E>>,
}
type MyGraph = Graph<MyNode, MyEdge>;
fn main() {
let x: MyGraph::N;//XXX
println!("Results:")
}
失敗,錯誤編譯:
error[E0223]: ambiguous associated type
--> /home/xxx/.emacs.d/rust-playground/at-2017-07-26-164119/snippet.rs:21:12
|
21 | let x: MyGraph::N;
| ^^^^^^^^^^ ambiguous associated type
|
= note: specify the type using the syntax `<Graph<MyNode, MyEdge> as Trait>::N`
有沒有辦法從Graph<MyNode, MyEdge>
得到N
類型?
我創建了一個別名(type =
)不重複的節點類型定義, 所以它會在標記XXX
點是巨大的,我可以不寫,但let x: MyNode
let x: expression with MyGraph as argument
。
Th似乎不必要的複雜;爲什麼不只是'讓x:MyNode;'?有多種節點類型? – ljedrz
@ljedrz嗯,爲了防止代碼重複,在我的程序中有'let x:MyNode'這樣的地方,當我更改此圖的Node類型時,我必須修復所有這些地方。 – user1244932