Skip to content

Syntax for multiple attributes #2516

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
camden-smallwood-zz opened this issue Aug 5, 2018 · 3 comments
Closed

Syntax for multiple attributes #2516

camden-smallwood-zz opened this issue Aug 5, 2018 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@camden-smallwood-zz
Copy link

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.

@petrochenkov
Copy link
Contributor

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

@Havvy
Copy link
Contributor

Havvy commented Oct 7, 2018

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.

@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Oct 10, 2018
@Centril
Copy link
Contributor

Centril commented Dec 22, 2018

Closing in favor of #2600.

@Centril Centril closed this as completed Dec 22, 2018
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

4 participants