struct - How to set generic trait as type of function argument? -


there generic trait graph

type nodekey = usize; type edgeweight = usize;  trait graph<t> {     fn add_node(&mut self, node: t) -> nodekey;     fn add_edge(&mut self, begin: nodekey, end: nodekey, weight: edgeweight);     fn new() -> self; } 

and implementation adjacencylist struct

struct adjacencylist<t> {     // ... } impl<t> graph<t> adjacencylist<t> {     // ... } 

i need function takes empty graph , it.

fn create_sample_graph<t: graph<&'static str>>(graph: &mut t) {     let key1 = graph.add_node("node1");     // ... } 

i create instance of adjacencylist &str type , send function.

fn main() {     let mut adjacency_list = adjacencylist::<&str>::new();     create_sample_graph(adjacency_list); } 

but compiler fails following error:

error: mismatched types:  expected `&mut _`,     found `adjacencylist<&str>` (expected &-ptr,     found struct `adjacencylist`) [e0308] create_sample_graph(adjacency_list);                     ~~~~~~~~~~~~~~ 

how can set trait type of function's argument , pass there struct implements trait?

you need pass &mut adjacency_list. that's error saying, , it's correct: you've defined function taking &mut pointer, you're passing value directly.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -