Rust/WinRT Public Preview now available on GitHub

    Rust/WinRT Public Preview now available on GitHub

    Rust/WinRT Public Preview now available on GitHub


    Posted: 30 Apr 2020

    We are excited to announce that the Rust/WinRT project finally has a permanent and public home on GitHub:

    GitHub - microsoft/winrt-rs: Rust/WinRT - Rust for the Windows Runtime


    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:

    Code:
    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:

    Code:
    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. GitHub - robmikh/minesweeper-rs: A port of robmikh/Minesweeper using winrt-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. GitHub - microsoft/winrt-rs: Rust/WinRT - Rust for the Windows Runtime


    Source: Rust/WinRT Public Preview - Windows Developer Blog


    Brink's Avatar Posted By: Brink
    30 Apr 2020


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 21:33.
Find Us




Windows 10 Forums