expression

XML elements to Rust fields

Decode a small XML record into a struct using the quick-xml serialize feature. Element names map to fields in this simple shape. This example uses external crates; see the staged Cargo manifest. Expected stdout (not executed): Sky: 120 Common mistake: Assuming deserialization checks an entire XML schema or validates every root name.

Formal Syntax & Structure

rust syntaxrust
quick_xml::de::from_str::<T>(xml)

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Deserialize)]    struct Book { title: String, pages: u32 }    let b: Book = quick_xml::de::from_str("<book><title>Sky</title><pages>120</pages></book>").unwrap();    println!("{}: {}", b.title, b.pages);}
Rust Reference