expression

Validate a dynamic header value

Validate dynamic field values before building messages. A syntactically valid value still needs field-specific interpretation and length limits. The negative sample is just an invalid newline-containing value. External crate example; constructs data in memory, does not send requests. Expected stdout (not executed): true true Common mistake: Assuming valid header syntax establishes authorization or the meaning of the field.

Formal Syntax & Structure

rust syntaxrust
http::HeaderValue::from_str(text) -> Result<HeaderValue, InvalidHeaderValue>

Working Example

Example Snippetrust
fn main() {    use http::HeaderValue;    println!("{}", HeaderValue::from_str("report-7").is_ok());    println!("{}", HeaderValue::from_str("line\nnext").is_err());}
Rust Reference