BT

Facilitating the Spread of Knowledge and Innovation in Professional Software Development

Write for InfoQ

Topics

Choose your language

InfoQ Homepage News Rust/WinRT Brings Microsoft Closer to Adopting Rust Internally

Rust/WinRT Brings Microsoft Closer to Adopting Rust Internally

This item in japanese

Bookmarks

Now available in preview, Rust/WinRT is a language projection for the Windows Runtime that enables calling Windows APIs in a natural and idiomatic way, similarly to other language projections such as C++/WinRT.

Rust/WinRT lets you call any WinRT API past, present, and future using code generated on the fly directly from the metadata describing the API and right into your Rust package where you can call them as if they were just another Rust module.

Using Rust/WinRT, Rust developers will be able to create apps and components for Windows as well as an NT service or a Windows driver, explains Microsoft principal software engineer Kenny Kerr. This is how you can parse an XML document using the XmlDocument class:

use windows::data::xml::dom::*;

let doc = XmlDocument::new()?;
doc.load_xml("<html>hello world</html>")?;

let root = doc.document_element()?;
assert!(root.node_name()? == "html");
assert!(root.inner_text()? == "hello world");

Microsoft's effort to make Rust/WinRT-based code look as much as possible like idiomatic Rust code is well exemplified by the automatic conversion from snake case to camel case and support for Rust null coalescing operator ? to simplify error propagation in the previous code snippet.

As InfoQ reported, Microsoft has been exploring Rust as the solution to write safe software for some time. Microsoft engineers Ryan Levick and Sebastian Fernandez explained at the last Barcelona RustFest that Microsoft's expectation here is that Rust will enable writing performant security critical components safely, although the Rust community still has a number of hurdles to overcome to give Rust and its toolchain first-class support on Windows.

This new project seems to confirm Microsoft is serious about using Rust wherever it makes sense for its own development:

Microsoft has long depended on C++ as the backbone for so much of what we do, but it has some challenges, particularly when it comes to security. [...] Rust is an intriguing language. It closely resembles C++ in many ways, hitting all the right notes when it comes to compilation, runtime model, type system and deterministic finalization. While it has its own unique learning curve, it also has the potential to solve some of the most vexing issues that plague C++ projects, and is designed from the ground up with memory safety and safe concurrency as core principles.

To use Rust/WinRT in a project, you add it as a dependency in your Cargo.toml file:

[dependencies]
winrt = { git = "https://github.com/microsoft/winrt-rs" }

This will allow you to import any Windows module:

use winrt::*;

import!(
    dependencies
        os
    modules
        "windows.data.xml.dom"
        "windows.foundation"
        "windows.ui"
);

Rust/WinRT is still an early preview, although it is already enough mature to power Robert Mikhayelyan's Minesweeper clone, which also provides a great intro to its usage.

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT