rust - How do I decode a URL and get the query string as a HashMap? -


i have url this:

http%3a%2f%2fexample.com%3fa%3d1%26b%3d2%26c%3d3 

i parsed hyper::url::parse , fetch query string:

let parsed_url = hyper::url::parse(&u).unwrap(); let query_string = parsed_url.query(); 

but gives me query string. want query string hashmap. this:

// code convert query string hashmap hash_query.get(&"a"); // eq 1 hash_query.get(&"b"); // eq 2 

there few steps involved:

  • the .query_pairs() method give iterator on pairs of cow<str>.

  • calling .into_owned() on give iterator on string pairs instead.

  • this iterator of (string, string), right shape .collect() hashmap<string, string>.

putting together:

let parsed_url = url::parse("http://example.com/?a=1&b=2&c=3").unwrap(); let hash_query: hashmap<_, _> = parsed_url.query_pairs().into_owned().collect(); assert_eq!(hash_query.get("a"), "1"); 

note need type annotation on hash_query—since .collect() overloaded, have tell compiler collection type want.


Comments

Popular posts from this blog

'hasOwnProperty' in javascript -

python - ValueError: No axis named 1 for object type <class 'pandas.core.series.Series'> -

java - How to provide dependency injections in Eclipse RCP 3.x? -