Rfcs: Syntax for multiple attributes

Created on 5 Aug 2018  路  3Comments  路  Source: rust-lang/rfcs

Hello, I've been experimenting with Rust since around the 0.3 release, but this is my first time posting an issue. I tried looking to see if there was already an open issue related to this, but I couldn't find anything.

Being a long-time C# programmer, I have a lot of code written in C# that I've transcribed to Rust. Attributes are a language feature that C# and Rust share in common, yet I've always found Rust's syntax for declaring attributes to be clunky and limiting.

Consider the following Rust code:

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct Point2D {
  pub x: f32,
  pub y: f32
}

If I were to write that same code in C# syntax, it would look somewhat like this pseudocode:

[Repr(C)]
[Derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
public struct Point2D
{
    public float X;
    public float Y;
}

However, C# allows you to condense attributes into a single pair of square brackets, like so:

[Repr(C), Derive(Clone, Copy, Debug, PartialEq, PartialOrd)]

This allows us to save a line of text, and removes some noise from the screen. I wrote this issue because I wanted to propose that Rust should implement a similar setup for attribute declaration, making both of the following be valid syntax and having the same effect on code:

#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct A;

#[repr(C), derive(Clone, Copy, Debug, PartialEq, PartialOrd)]
pub struct A;

If this was brought up in the past and declined, I apologize. Thank you for taking the time to check this out.

T-lang

Most helpful comment

I plan to write an RFC for this once I get RFC #2539 implemented.

For more prior art, C has __attr__() with multiple attributes as well.

All 3 comments

https://github.com/rust-lang/rust/pull/50120 restricted the attribute grammar from arbitrary token streams to delimited token trees, so this should be possible to do now.

I plan to write an RFC for this once I get RFC #2539 implemented.

For more prior art, C has __attr__() with multiple attributes as well.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

glaebhoerl picture glaebhoerl  路  112Comments

rust-highfive picture rust-highfive  路  75Comments

pnkfelix picture pnkfelix  路  253Comments

rust-highfive picture rust-highfive  路  103Comments

Centril picture Centril  路  90Comments