Rust 2021 | features and trait bounds in macros

Thu 24 September 2020

Filed under Programming

Tags Rust

It's that time of the year again where we all gather around the campfire to muse on the future of this language we love. Last year I didn't blog anything because I was very new to Rust. I started learning about it in the summer of 2019, but the first real code I wrote was in the fall. I have loved it ever since and was lucky that I was able to write Rust code at work. (Everything is in C#, but I got to start an embedded project)

So to summarize:

  • I use Rust mostly for embedded and the accompanying server.
  • I've been using Rust almost full-time for a little over a year.

Tooling

This has gotten a lot better in the last year. The IntelliJ plugin has improved and Rust Analyzer came into being.

Specifically for embedded, there's the probe.rs project and also the Knurling project. Both show great promise of what embedded development can become!

But there is an improvement I'd like to see to the tooling.

Suggestion

Show crate features on crates.io and allow us to document them for docs.rs. This would improve the crate discovery experience for me. Often I want to know which features I can turn on, or rather, which I can turn off like std.

Language

I've been working with macros lately and they're so much better than the ones found in C.

The variables all have meta types. An example:

macro_rules! foo {
    (x => $e:expr) => (println!("mode X: {}", $e));
    (y => $e:expr) => (println!("mode Y: {}", $e));
}

Each branch of this macro has a variable 'e' that is an expression. There are many more of these meta types like path and ty.

Suggestion

I would like to see some more. Most notably I want to see something that encodes trait bounds.

Imagine this:

macro_rules! create_foo {
    ($b:bounds) => {
        pub struct Foo<I: $b> {
            pub bar: I,
        }
    };
}

create_foo!(Debug + Copy + Clone)

Why would this be special? Well, currently this is very difficult to express! Bounds can come in many forms, so if you want to use this in a macro you've got two choices. Using a tt token tree to capture everything or elaborately encoding all possible options in the macro branch.

Using tt limits your macro in a lot of ways. The other option isn't any better.

End

Thanks for reading!


Comments


Carmine Crystal Blog © Dion Dokter Powered by Pelican and Twitter Bootstrap. Icons by Font Awesome and Font Awesome More