We are excited to announce that the Rust/WinRT project finally has a permanent and public home on GitHub:
Rust/WinRT follows in the tradition established by C++/WinRT of building language projections for the Windows Runtime using standard languages and compilers, providing a natural and idiomatic way for Rust developers to call Windows APIs. 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.
The Windows Runtime is based on Component Object Model (COM) APIs under the hood and is designed to be accessed through language projections like C++/WinRT and Rust/WinRT. Those language projections take the metadata describing various APIs and provide natural bindings for the target programming language. As you can imagine, this allows developers to more easily build apps and components for Windows using their desired language. You can then use those Windows APIs to build desktop apps, store apps, or something more unique like a component, NT service, or device driver.
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. Modern C++ certainly makes it easier to write safe and secure C++ if you follow certain careful conventions, but that is often hard to enforce on larger projects. 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. For more information on Rust and safe systems programming, check out the Microsoft Security Response Center.
Here below is a simple example of Rust calling a Windows API. The API itself does not matter, but it should give you a sense for how naturally Windows APIs may be called from Rust code. In the following example, we are using the XmlDocument class from the Windows.Data.Xml.Dom namespace to parse and inspect a simple XML document:
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");
If you are familiar with Rust, you will notice this looks far more like Rust than it looks like C++ or C#. Notice the snake_case on module and method names and the ? operator for error propagation.
Here is another example using the Windows.ApplicationModel.DataTransfer namespace to copy some value onto the clipboard:
use windows::application_model::data_transfer::*; let content = DataPackage::new()?; content.set_text("Rust/WinRT")?; Clipboard::set_content(content)?; Clipboard::flush()?;
For a more complete example, I encourage you to have a look at Robert Mikhayelyan’s Minesweeper demo. Robert originally wrote a version of the classic game using C++/WinRT and was able to quickly port it over to using Rust/WinRT. https://github.com/robmikh/minesweeper-rs
This is a very early public preview, but we have decided to work in the open from here on out. So please give it a try and let us know what you think. We would love the feedback as we continue to develop Rust/WinRT and plan to eventually publish on crates.io. We also hope to provide more seamless interop with existing Win32 and COM APIs including support for the com-rs crate, which supports COM APIs today. https://github.com/microsoft/winrt-rs