rust - How do I specify that a function takes a HashMap? -


how specify non-primitive type rust function parameter - specifically, hashmap? example:

use std::collections::hashmap;  // main function call fibbonacci...  // here hashmap used memoizing; // maybe ugly, it's first attempt fn fibbonacci(n: i32, cache: ??) -> i32 { } 

i've tried:

  • cache: hashmap => wrong number of type arguments: expected @ least 2, found 0
  • cache: <hashmap> => error: expected ::, found )
  • cache: std::collections::hashmap => wrong number of type arguments: expected @ least 2, found 0

dog wearing tie paws on computer keyboard , tongue hanging out - "i have no idea i'm doing"

this rust 1.0.0.beta.

let's check out compiler error message code:

use std::collections::hashmap;  fn fibbonacci(n: i32, cache: hashmap) -> i32 {}  fn main() {} 

we get:

error[e0243]: wrong number of type arguments: expected @ least 2, found 0  --> src/main.rs:3:29   | 3 | fn fibonacci(n: i32, cache: hashmap) -> i32 {}   |                             ^^^^^^^ expected @ least 2 type arguments 

note points directly issue , tells you need 2 type arguments. rust requires function arguments , return values spelled out, there no type inference @ point.

i don't know want keys , values be, i'll assume i32:

fn fibonacci(n: i32, cache: hashmap<i32, i32>) -> i32 { 0 } 

more verbosely, hashmap has 2 generic type parameters, referred k , v (but see note below). reference concrete type of hashmap, need specify k , v are. can use more generic types place trait bounds on generics. bit more advanced, , don't need worry started rust!

note - hashmap has 3 type parameters, third has default value , isn't used. type parameter allows controlling hashing algorithm used.


Comments

Popular posts from this blog

Email notification in google apps script -

c++ - Difference between pre and post decrement in recursive function argument -

javascript - IE11 incompatibility with jQuery's 'readonly'? -