I'm having trouble converting from an Iterator of (String, String)
to an Iterator of (&str, &str)
. I'm using an external library, so can't change the signature of that, and not sure that I need to. Basically I have this function def:
use hyper;
fn build_url<'a, I>(host: &'a str, port: u16, path: &'a str, params: I) ->
hyper::Url where I: Iterator<Item=(String, String)> {
let mut url = hyper::Url::parse(&format!("http://{h}:{p}/{pt}",
h = self.etcd_host,
p = self.etcd_port,
pt = path));
if let Err(e) = url {
panic!("error parsing url: {}", e);
}
let mut url = url.unwrap();
// fn set_query_from_pairs<'a, I>(&mut self, pairs: I)
// where I: Iterator<Item=(&'a str, &'a str)>
url.set_query_from_pairs(
params.map(|x: (String, String)| ->
(&str, &str) { let (ref k, ref v) = x; (k, v) } ));
}
But I'm getting the dreaded: error: 'x.0' does not live long enough
I think the ref
keyword in the let should have been the right thing here, i.e. keep the ownership with the Iterator, and just do a borrow. I get a similar issue if I get rid of ref
in the let changing the let to this:
let (k, v) = x; (&k, &v)
Then k
and v
don't live long enough. Does anyone have a recommendation for fixing this?
params
iterator from? is it a map or a vector? If so, you can create a non-owning iterator from that and then map it to a(&str, &str)
iterator