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

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -