Path inference syntax (.Variant, .{ … }) —

⚓ Rust    📅 2025-12-14    👤 surdeus    👁️ 19      

surdeus

Warning

This post was published 54 days ago. The information described in this article may have changed.

For some time now, I have wanted path inference in Rust to reduce repetition when constructing enums and structs. As such, RFC 3444 proposes leading-dot syntax for this. This allows you to write the following:

set_wireless_config(.{
    wlan: .AccessPoint,
    bluetooth: .Enabled
})

I implemented a prototype of this in the Rust compiler and I'm now looking for feedback on how it feels to actually use.

Links

Questions

  • Would you use this feature when writing Rust?
  • Does writing using this syntax feel natural?
  • If you were code reviewing, would this syntax harm your understanding of the code significantly?

Try It

Build the compiler:

git clone -b 3444 https://github.com/JoshBashed/rust.git
cd rust
./x.py build library

Create a test.rs:

#[derive(Debug)]
enum Fruit {
    Apple,
    Banana,
    Blueberry,
    Grape,
}

impl Fruit {
    fn color(&self) -> &'static str {
        match self {
            .Apple => "green",
            .Banana => "yellow",
            .Blueberry => "blue",
            .Grape => "purple",
        }
    }
}

fn main() {
    let fruit: Fruit = .Apple;
    println!("{:?} is {}", fruit, fruit.color());
}

Build and run:

build/<your-platform-triple>/stage1/bin/rustc test.rs
./test

1 post - 1 participant

Read full topic

🏷️ Rust_feed