Skip to content

Attributes: Redundant syntax, unnecessary complication #31001

Closed
@prasannavl

Description

@prasannavl

Well, I'm quite new to rust, so its entirely possible I'm missing something important. But I'm curious to know why the team chose to have two different type of attributes, and comments as well.

One is of the form #[attr], while the other is #![attr].

The former is straightforward, but the latter is for the item that its enclosing.

Taking the example right out of the docs

#[foo]
struct Foo;

mod bar {
    #![bar]
}

Is there a necessity for this redundant complication? I see no reason why the same #[foo] can't be applied right outside the module.

i.e,

#[foo]
mod bar {
    ...
}

Now, foo will apply into the scope. From what I can surmise, the only reason I can come up is, for scenarios like these:

let x = |x| {
 #[foo for this block]
}

But this can also be quite easily achieved with the compiler, this way,

let x =  #[foo] |x| {
 ...
}

Similar to how attributes are handled in C# for example. I see it from the perspective that a little more complication in the compiler to be able to resolve these would definitely be better than complicating the whole language by adding a language syntax that seems entirely redundant. And when it comes to readability, the one is actually quite better than a whole new syntax for achieving the same thing, because the intent is more consistent.

Same goes for comments, for the module as well. Can't this just be simplified? Rust already has a complicated enough learning curve, and these seem completely unnecessary. I'm hoping that I completely missed something, and I'm wrong :) .. or this is not the most stellar design choice that should be rectified as soon as possible while the rust ecosystem is still in its infancy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language teamrust-2-breakage-wishlistIn the wishlist of breaking changes that requires rust 2.0

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions