use reqwest::header::USER_AGENT;
let client = reqwest::blocking::Client::new();
let res = client.get("https://www.rust-lang.org")
.header(USER_AGENT, "foo")
.send()?;
I got it that USER_AGENT is defined as a const of HeaderName.
I would like to know how the string slice "foo" is translated to HeaderValue.
pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
So to call the function, the HeaderValue type must implement the TryFrom<V> trait. If you check out the Trait Implementations section on HeaderValue, you will see that an impl for V = &str indeed does exist.
impl<'a> TryFrom<&'a str> for HeaderValue
So the string slice "foo" is converted to a HeaderValue through that TryFrom impl.
It looks for me more than the "pattern" substitution.
pub fn header<K, V>(self, key: K, value: V) -> RequestBuilder where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<Error>,
<HeaderValue as TryFrom<V>>::Error: Into<Error>,
```
HeaderName is a stuct and here it means it implements TryFrom<K>.
But match of pattern is not clear to me.
The <HeaderValue as TryFrom<V>>::Error syntax would in this case evaluate to InvalidHeaderValue from the impl block above. So the last bound says that if conversion into header value fails, the error must be convertible into hyper's Error type.