pub struct Headers(/* private fields */);Expand description
A list of key-value pairs representing HTTP headers.
// Prepares a new Headers instance with a specified capacity
let mut headers = Headers::with_capacity(2);
headers.add("Content-Type", "text/plain");
headers.add("Authorization", "Bearer token");
assert_eq!(headers.len(), 2);
assert_eq!(headers.first("Content-Type"), Some("text/plain"));
// Supports iterating over headers for updates
for header in headers.iter_mut() {
if header.key.eq_ignore_ascii_case("Authorization") {
header.value = String::from("SECRET");
}
}
// Key lookup is case-insensitive
assert_eq!(headers.first("authorization"), Some("SECRET"));
// Accessing headers by index
assert_eq!(headers[0].key, "Content-Type");
assert_eq!(headers[0].value, "text/plain");
// Creates a Headers instance from an iterator of tuples
let headers = Headers::from_iter([
("X-Custom-Header", "value1"),
("X-Another-Header", "value2")
]);
assert_eq!(headers.first("x-custom-header"), Some("value1"));
assert_eq!(headers.first("x-another-header"), Some("value2"));Implementations§
Source§impl Headers
impl Headers
Sourcepub fn with_capacity(capacity: usize) -> Self
pub fn with_capacity(capacity: usize) -> Self
Prepares a new Headers instance with a specified capacity.
Sourcepub fn values_for(&self, key: &str) -> impl Iterator<Item = &str>
pub fn values_for(&self, key: &str) -> impl Iterator<Item = &str>
Provides an iterator over all header values for a given key.
Sourcepub fn first(&self, key: &str) -> Option<&str>
pub fn first(&self, key: &str) -> Option<&str>
Provides the first value for a given key, if it exists.
Sourcepub fn add(&mut self, key: &str, value: &str)
pub fn add(&mut self, key: &str, value: &str)
Adds a new header line with the specified key and value.
Sourcepub fn insert_many<I, T>(&mut self, iter: I)
pub fn insert_many<I, T>(&mut self, iter: I)
Adds a new header line from a tuple of key and value.
Sourcepub fn iter(&self) -> impl Iterator<Item = &HeaderLine>
pub fn iter(&self) -> impl Iterator<Item = &HeaderLine>
Reference iterator for the headers collection.
Sourcepub fn iter_mut(&mut self) -> impl Iterator<Item = &mut HeaderLine>
pub fn iter_mut(&mut self) -> impl Iterator<Item = &mut HeaderLine>
Mutable reference iterator for the headers collection.
Trait Implementations§
Source§impl<T> FromIterator<T> for Headerswhere
T: Into<HeaderLine>,
impl<T> FromIterator<T> for Headerswhere
T: Into<HeaderLine>,
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Creates a value from an iterator. Read more
Source§impl<'a> IntoIterator for &'a Headers
impl<'a> IntoIterator for &'a Headers
Source§impl IntoIterator for Headers
impl IntoIterator for Headers
impl Eq for Headers
impl StructuralPartialEq for Headers
Auto Trait Implementations§
impl Freeze for Headers
impl RefUnwindSafe for Headers
impl Send for Headers
impl Sync for Headers
impl Unpin for Headers
impl UnsafeUnpin for Headers
impl UnwindSafe for Headers
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more