0

Rust – A Convenient Way to Write Out Start and End XML Tags using Closure

Did you miss writing out an XML element end tag in your Rust codes? There is a convenient way to avoid this in Rust using Closure.

Coming From Java

In Java, a similar implementation can be achieve using Lambda. For more information, please see A More Convenient Way to Write Out Start and End XML Tags in Java using Lambda

Requirements

The following were used for this post.

  • Rust 1.38.0
  • Cargo
  • xml-rs 0.8.0
    • Used for writing XML files
  • Intellij IDEA
  • Rust plugin for Intellij IDEA

Writing XML is Ugly

Without using Closures, do we want write XML with these codes? Do we always need to specify the closing tags?

Writing XML using Closure

Update Cargo.toml

Main.rs

First, import the stuff we need.

Second, create a function that accepts a closure among other things.

Next, create a function that makes calls to write_element_block  function.

Finally, the main function.

This writes XML data to a file output.xml with the following contents.

Karl San Gabriel

Karl San Gabriel

Professional Software Developer