Rust: getting started

So, you’re a C++ developer and want to learn Rust. You’ve come to the right place. 😊 It’s not hard to get started with Rust, but as a C++ developer and especially one with a preference for Windows you are likely to run into some of the same challenges I have. I hope that sharing my experience will help you to get started with Rust a little more quickly than I did.

On Windows, Rust requires the Microsoft C++ build tools. You guessed right: Rust depends on the thing you love most about C++… the linker! And a few other things… You can download the C++ build tools separately, or just install Visual Studio 2019. I recommend the latter.

https://visualstudio.microsoft.com/downloads/

Visual Studio provides a dizzying array of options, most of which are completely irrelevant to the C++ developer. You can painstakingly pick any of the “individual components” the installer provides or just pick some “workloads”. I suggest you do the latter. Here’s what I do to keep things simple.

That’s right. Pick all three “Windows” workloads. You may not think you need them all but invariably, some dependency will arise where these are required, and this just keeps things simple. The only “individual component” I add to the mix is “Git for Windows”. That’s important because, well, Git rules the world and you’re going to need that real soon.

You can scroll through the endless list or just use the search box to narrow things down. And that’s it. Hit the install button and then go and grab a coffee.

Once Visual Studio is installed, you can head over to the Rust website to get started.

https://www.rust-lang.org/

You should notice the prominent “Get Started” button. Go ahead and click that. You should then see a few options. You can for example “try Rust without installing” but what’s the fun in that. Instead, go for the “RUSTUP-INIT.EXE” button that links to https://win.rustup.rs/ and run the resulting executable. This will pop up a console installer.

Go ahead and pick option 1 and the installer will get on with downloading the various Rust build tools. Soon enough that will be done and you will be ready to create your first project!

To get started you need a console window. You can use any command prompt, but I suggest using the Visual Studio tools command prompt. You can then confirm that Rust is installed with a simple version command: cargo –version

What’s cargo? Well you could also run the same command with rustc, the Rust compiler, but to be honest you’ll almost never run the Rust compiler directly. So, just get used to running Cargo for all your build needs. Cargo is officially Rust’s package manager, whatever that means. Package managers may be a little foreign to the average C++ developer. Unlike C++, Rust has an integrated packaging story and Cargo is the tool that you use to manage packages. It might help if I told you that practically every Rust project is a package. In that light, Cargo is really the Rust project manager. You can use Cargo to create projects, build projects, test projects, publish projects, and much more. It’s just that Rust projects are called packages… Except when they’re called crates, but that’s enough about that confusing topic for one day.

As I mentioned, rustc is the Rust compiler. We can quickly put Hello World behind us as follows. Create a text file, with the .rs extension, and include a simple main function:

C:\hello_world>type app.rs

fn main() {
    println!("Hello world!");
}

Not a fan of that formatting? You’re not alone, but more about formatting later. You can now compile this program as follows:

C:\hello_world>rustc app.rs

And lo and behold you’ve created your first Rust program:

C:\hello_world>dir

    150,528 app.exe
  1,363,968 app.pdb
         49 app.rs

Notice there’s both an executable and a .pdb file for debugging. Naturally, you can simply run the executable from the console:

C:\hello_world>app
Hello world!

OK, you’ve confirmed that Rust is installed and managed to compile your first bit of Rust code. Awesome! What about Cargo and building actual projects? Please tell me there’s an IDE and Notepad isn’t going to be my Rusty lot in life? What about testing and debugging? Oh there’s so much to explore. So join me next time for the adventures of a C++ developer learning Rust.

My Rust adventure begins

I have come to the point with C++/WinRT where I am largely satisfied with how it works and leverages C++ to the best of its ability. There is always room for improvement and I will continue to evolve and optimize C++/WinRT as the C++ language itself advances. But as a technology, the Windows Runtime has always been about more than just one language and we have started working on a few different projects to add support for various languages. None of these efforts could however draw me away from C++… that is until Rust showed up on my radar.

Rust is an intriguing language for me. It closely resembles C++ in many ways, hitting all the right notes when it comes to compilation and runtime model, type system and deterministic finalization, that I could not help but get a little excited about this fresh new take on language design. I have spent almost every waking moment over the last few months (that I’m not hanging out with my family) exploring, studying, and experimenting with Rust. I looked for the usual signs of a language that is not really geared for a systems programmer like myself, but found none. To the contrary, I found that while it has its own unique and dramatic learning curve, it also has the potential to solve some of the most vexing issues with C++’s relationship to WinRT. Imagine C++/WinRT without any need for IDL, faster build times, and a simple and integrated build system.

And so it is that I have started building the WinRT language projection for Rust. I’m just getting started and have much to learn, but the plan is to build complete and deep support for WinRT in a way that is natural and familiar for the Rust developer. This is not going to look very much like C++/WinRT because idiomatic Rust does not look and feel like C++, but I plan to apply the same level of rigor in producing WinRT support for Rust that is both very efficient and a joy to use.

I’ll be sharing more about my adventures with Rust right here on kennykerr.ca but if you’d like to follow along more closely, take a look at the Rust winmd parser I wrote to get things started:

https://github.com/microsoft/winmd-rs

This is largely based on the C++ winmd parser library. While certainly not complete, it has just enough in place to allow me to now spend some time exploring and laying the groundwork for the WinRT support. The plan is to turn this Rust crate into a complete winmd parser for both reading and generating winmd files. A separate Rust crate will then provide the actual support for consuming and producing WinRT APIs.

But I’m getting ahead of myself. Do let me know what you think. I’d love to hear from you. And don’t forget to check back soon as I will probably start writing about the adventures of a C++ developer learning Rust. 🙂

C++/WinRT and xlang repos

If you follow along on GitHub, you may have noticed a few changes in the C++/WinRT and xlang world. It became clear that having one repo for a variety of projects and languages just wasn’t practical. Developers interested in working on one language or library inevitably had to deal with all of it, creating an unnecessarily steep learning curve. And while we have a lot of ambitions for the xlang project, its clear that C++/WinRT remains our flagship project. To that end, and to make it easier to work with the two most popular projects under the xlang umbrella, we’ve split the projects up as follows:

C++/WinRT
Repository: https://github.com/microsoft/cppwinrt
Documentation: https://aka.ms/cppwinrt
NuGet package: http://aka.ms/cppwinrt/nuget
Visual Studio extension: http://aka.ms/cppwinrt/vsix

C++ winmd parser library
Repository: https://github.com/microsoft/winmd
NuGet package: http://aka.ms/winmd/nuget

Existing projects related to cross-platform support
Repository: https://github.com/microsoft/xlang

You may also have noticed some new GitHub repos for Java and C# language support. Obviously, we’d love to add support for every popular language, but our resources are limited. We have experimented with adding support for both. The C# project might seem a little curious given that C# currently supports WinRT directly, but we have discovered through our experience with C++/WinRT that we can provide a far better experience by separating the WinRT support from the compiler itself. On a personal note, I’m spending a lot of my time working with the Rust language and can’t wait to share more about that eventually. 😉

The Old New Thing on C++/WinRT

In case you haven’t noticed, Raymond Chen has joined me in writing about C++/WinRT. He has published a great collection of tips and tricks internally and plans to publish them all publicly as time allows. Here are a few to get you started:

Detecting whether the -opt flag was passed to cppwinrt.exe: Using __has_include

How can I determine in a C++ header file whether C++/CX is enabled? How about C++/WinRT?

Why does my C++/WinRT project get errors of the form “Unresolved external symbol void* __cdecl winrt_make_YourNamespace_YourClass(void)“?

Why does my C++/WinRT project get errors of the form ‘winrt::impl::produce‘: cannot instantiate abstract class, missing method GetBindingConnector

Why does my C++/WinRT project get errors of the form “consume_Something: function that returns ‘auto’ cannot be used before it is defined”?

Why does my C++/WinRT project get errors of the form “unresolved external symbol … consume_Something”?

Windows Runtime delegates and object lifetime in C++/WinRT

Meet C++/WinRT 2.0: resume_foreground Improvements

It turns out that resume_foreground was being a little too clever and could introduce deadlocks in some scenarios because it only suspended if not already on the dispatcher thread. This seemed like a good idea at the time, but being able to depend on stack unwinding and re-queuing turns out to be quite important for system stability especially in OS code. Consider this simple example:

fire_and_forget UpdateAsync(TextBlock block)
{
    co_await resume_background();
    hstring message = L"Hello developers!";

    co_await resume_foreground(block.Dispatcher());
    block.Text(message);
}

Here we’re performing some complex calculation on a background thread and then naturally switch to the appropriate UI thread before updating some UI control. The resume_foreground function had some cleverness that looked something like this:

auto resume_foreground(...) noexcept
{
    struct awaitable
    {
        bool await_ready() const
        {
            return m_dispatcher.HasThreadAccess(); // <-- Cleverness...
        }
        void await_resume() const {}
        void await_suspend(coroutine_handle<> handle) const { ... }
    };
    return awaitable{ ... };
};

This has been updated as follows:

auto resume_foreground(...) noexcept
{
    struct awaitable
    {
        bool await_ready() const
        {
            return false; // <-- Queue without waiting
        }
        void await_resume() const {}
        void await_suspend(coroutine_handle<> handle) const { ... }
    };
    return awaitable{ ... };
};

This is analogous to the difference between SendMessage and PostMessage in classic desktop app development. The latter will queue the work and then unwind the stack without waiting for it to complete. This unwinding the stack can be essential.

The resume_foreground function also initially only supported the CoreDispatcher tied to a CoreWindow that was originally introduced with Windows 8. A more flexible and efficient dispatcher has since been introduced. The DispatcherQueue is nice in that you can create them for your own purposes. Consider a simple console app:

using namespace Windows::System;

fire_and_forget RunAsync(DispatcherQueue queue);

int main()
{
    auto controller = DispatcherQueueController::CreateOnDedicatedThread();
    RunAsync(controller.DispatcherQueue());
    getchar();
}

Here I’m creating a private queue thread and then passing this queue object to the coroutine. The coroutine can then presumably use it to await – suspend and resume on this private thread. Another common use of the DispatcherQueue is to create a queue on the current UI thread for a traditional desktop or Win32 app.

DispatcherQueueController CreateDispatcherQueueController()
{
    DispatcherQueueOptions options
    {
        sizeof(DispatcherQueueOptions),
        DQTYPE_THREAD_CURRENT,
        DQTAT_COM_STA
    };

    ABI::Windows::System::IDispatcherQueueController* ptr{};
    check_hresult(CreateDispatcherQueueController(options, &ptr));
    return { ptr, take_ownership_from_abi };
}

Not only does this illustrate how Win32 functions may be called and incorporated into C++/WinRT projects, by simply calling the Win32-style CreateDispatcherQueueController function to create the controller and then transferring ownership of the resulting queue controller to the caller as a WinRT object, but this is precisely how you can support efficient and seamless queuing on your existing Petzold-style Win32 desktop app:

fire_and_forget RunAsync(DispatcherQueue queue);

int main()
{
    Window window;
    auto controller = CreateDispatcherQueueController();
    RunAsync(controller.DispatcherQueue());
    MSG message;

    while (GetMessage(&message, nullptr, 0, 0))
    {
        DispatchMessage(&message);
    }
}

This simple main function starts by creating a window. You can imagine this registers a window class and calls CreateWindow to create the top-level desktop window. The CreateDispatcherQueueController function is then called to create the queue controller before calling some coroutine with the dispatcher queue owned by this controller. A traditional message pump is then entered where resumption of the coroutine naturally occurs on this thread. Having done so, you can return to the elegant world of coroutines for your async or message based workflow within your app:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ... // Start on the calling thread

    co_await resume_foreground(queue);

    ... // Resume on the dispatcher thread
}

The resume_foreground will always “queue” and then unwind the stack. You can also optionally set the resumption priority:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    co_await resume_foreground(queue, DispatcherQueuePriority::High);

    ...
}

But if you only care about default queuing order then you can even await the queue itself and save yourself a few keystrokes:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    co_await queue;

    ...
}

For the control freaks out there, you can even detect queue shutdown and handle that gracefully:

fire_and_forget RunAsync(DispatcherQueue queue)
{
    ...

    if (co_await queue)
    {
        ... // Resume on dispatcher thread
    }
    else
    {
        ... // Still on calling thread
    }
}

The co_await expression will return true, indicating that resumption will occur on the dispatcher thread. In other words, queuing was successful. Conversely, it will return false to indicate that execution remains on the calling thread because the queue’s controller is shutting down and is no longer serving queue requests.

As you can see, you have a great deal of power at your fingertips when you combine C++/WinRT with coroutines and especially when you do some old-school Petzold style desktop app development.

And that’s all for today. I hope you enjoy using C++/WinRT!

Meet C++/WinRT 2.0: Fewer Dependencies

I’ve always loved tools like Sysinternals where there is a single executable that you can simply copy onto your dev box and run. No need for an installer or a carefully managed tree of DLLs. It just works. Well cppwinrt.exe is like that as well. From the start, you could simply copy it onto any Windows 10 machine and it would just work. Still, there’s always room for improvement. Have a look at the dependencies reported by dumpbin for version 1 of cppwinrt:

> dumpbin /dependents cppwinrt.exe

ADVAPI32.dll
SHELL32.dll
api-ms-win-core-file-l1-1-0.dll
api-ms-win-core-processthreads-l1-1-0.dll
XmlLite.dll
api-ms-win-core-libraryloader-l1-2-0.dll
api-ms-win-core-processenvironment-l1-1-0.dll
RoMetadata.dll
SHLWAPI.dll
KERNEL32.dll
api-ms-win-core-rtlsupport-l1-1-0.dll
api-ms-win-core-heap-l1-1-0.dll
api-ms-win-core-localization-l1-2-0.dll
api-ms-win-core-timezone-l1-1-0.dll
api-ms-win-core-console-l1-1-0.dll
OLEAUT32.dll
api-ms-win-core-winrt-error-l1-1-0.dll
api-ms-win-core-winrt-error-l1-1-1.dll
api-ms-win-core-winrt-l1-1-0.dll
api-ms-win-core-winrt-string-l1-1-0.dll
api-ms-win-core-synch-l1-1-0.dll
api-ms-win-core-threadpool-l1-2-0.dll
api-ms-win-core-com-l1-1-0.dll
api-ms-win-core-com-l1-1-1.dll
api-ms-win-core-synch-l1-2-0.dll

In my defense, that’s not as bad as it looks. All of those DLLs are shipped with Windows 10 and those api-ms-win-core-xxx entries are really forwarding DLLs that support API sets. Still, there was one DLL in that list that caused a bit of trouble. RoMetadata.dll provides the implementation of the metadata parser shipped with the operating system. This is the implementation that practically everyone uses either directly or indirectly. We first hit a snag with this because the rather locked down server SKU that the build engineers at Microsoft wanted to use didn’t include this DLL. That turned out to be a Windows setup bug, but it got me thinking more about dependencies.

With C++/WinRT 2.0 I finally started writing a completely independent metadata parser in standard C++ to avoid this dependency and solve all kinds of trouble with this clunky old parser. A few guys on the team chipped in and this parser is now the foundation for all of our modern tooling. I then also ditched the forwarding DLLs to the point where dumpbin now reports a slightly smaller set of dependencies for version 2 of cppwinrt:

> dumpbin /dependents cppwinrt.exe

KERNEL32.dll
ADVAPI32.dll
XmlLite.dll
SHLWAPI.dll

The fun thing about this is that all of those DLLs are available, not only on Windows 10, but all the way down to Windows 7 and even Windows Vista. That means if you happen to have some crazy old build server running Windows 7, well then you can still run cppwinrt to generate the C++ headers for your project. And if you actually want to run C++/WinRT on Windows 7 you can even do that with a bit of work as well.

And that’s all for today. I hope you enjoy using C++/WinRT!