Rust for Windows Achieves Full Consumption (Call any Windows API)
Microsoft's Rust for Windows dev team announced the new v0.9 update provides full consumption support, meaning the language is now capable of calling any Windows API.
That's done with a language projection, which lets developers interact with Windows Runtime (WinRT) APIs in ways natural -- or idiomatic -- to specific languages. Thus there are C++/WinRT, C#/WinRT and Rust/WinRT (and so on) language projections.
Microsoft's Rust/WinRT project -- now called Rust for Windows -- on GitHub says "The Rust language projection follows in the tradition established by C++/WinRT of building language projections for Windows using standard languages and compilers, providing a natural and idiomatic way for Rust developers to call Windows APIs."
The news with the v0.9 update, announced last week, is "full consumption support."
"With completed consumption support, you can now call any Windows APIs (past, present, and future) using the Rust language projection," the dev team announced. "Rust developers have access to the entire Windows API surface in a language-idiomatic way, allowing them to easily take advantage of the power and breadth of Windows development."
That's a big deal for the Rust community -- and Microsoft -- because the company has long been eyeing Rust as a safer alternative to C++ for low-level Windows programming. Rust is syntactically similar to C++ but has built-in protection against memory bugs that have long plagued developers working with the tricky and complicated C++. Thus Rust is sparking great interest in the .NET community, though its usage is still relatively low compared to other languages (see graphics below of recent .NET Foundation survey).
In announcing Rust for Windows last year, Microsoft said:
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.
In the new update, along with full API consumption, developers can find new documentation on Developing with Rust on Windows and a Rust for Windows (0.9) video from Kenny Kerr. As for nuts-and-bold coding functionality, Microsoft listed:
- Added support for Win32 and COM APIs, unifying the Windows APIs available for consumption via the windows crate. The addition of these APIs is enabled by the win32metadata project. With this increased coverage and unification of Windows APIs, we changed the name of the project from "Rust/WinRT" to "Rust for Windows."
- Added several examples to the Rust for Windows repo, demonstrating how to call a wide range of Windows APIs (includes Win32, COM, and WinRT APIs).
- The windows crate is published on crates.io and is now dual-licensed under MIT or Apache.
- The windows crate now uses generated bindings rather than hand-written bindings internally.
- The windows crate now builds on Linux.
- Many improvements and fixes for Win32 APIs, such as support for array types, a variety of string types, and updated metadata.
- Added more natural and idiomatic support for COM interfaces such as with return values, and support for additional APIs involving things like C-style unions and nested types.
- Improved build times and error handling.
- Original API case is now preserved, which will affect existing code using the windows crate.
- Transformed QueryInterface-like functions into generic functions, making it safer and more convenient to call many COM-related functions.
"We have made great progress with Rust for Windows over the last year, and we are continuing to invest in the project to make building Windows apps easy for Rust developers. Up next, we are working on Rust authoring support both for COM interfaces and WinRT components, so stay tuned," said Microsoft's Angela Zhang in last week's announcement conclusion.
David Ramel is an editor and writer for Converge360.