I just discovered today that attributes can be placed before expressions. Is there actual use for it in the real world?

The Rust Reference has a section about this: Expressions - The Rust Reference (section: "Expression Attributes")

I also tested it in the playground, and it's valid syntax (it emits warnings, but it runs): Rust Playground (/// text is syntax sugar for #[doc = " text"])

I wonder: Is there actual use for it (attributes for expression) in the real world?

I'm sorry I don't follow. Your playground link is broken, could you please share the right link? In the upper right-hand corner, press the share button. Copy the permalink to the playground to your clipboard and insert it here, please.

1 Like

I have fixed it. Thanks for telling me.

I think Rust Playground should copy from TypeScript playground: Reactively modifying the URI to reflect to code being written.

Typescript playground encodes the source code in base64 in the URL. Rust playground saves the code as a GitHub gist. Both have pros and cons, but I've more often than not been frustrated by TS playground because it won't go above like 4000 bytes or something.

3 Likes

I'm unclear what you're trying to describe. The section of the Reference that you linked to is about applying outer attributes (like #[foo]) to expressions, yet there are no attributes in the playground code (aside from a #[derive], which is applied to a struct declaration). I see nothing special about the playground code, and I'll note that all the warnings are about unused doc comments or struct fields.

1 Like

I think I already stated why I did what I did: I used #[doc] only to test if the syntax is valid. I don't know if there is actual attributes (which wouldn't be doc, obviously) that work with expression, hence the question.

It's still unclear what you are asking. Expressions can be used as expression statements (that's in fact one of the most common kind of statement), and you can put multiple, semicolon-separated statements in a block (because that is the whole point of a block). I don't see anything curious and/or useless about this.

1 Like

BTW, in case you didn't know, /// is but a syntactic sugar for #[doc].

I have an answer on my own: Compilation flags.

Apparently, normal macro calls cannot be applied to members inside a structure (array, tuple, struct) so attribute is necessary.

?

Macros can be used inside these types.

Is it possible you made a mistake in the title of the post?
I think you might have meant "I just discovered today that attributes can be placed before expressions. Is there actual use for it in the real world?"

5 Likes

Note that the field assignments in

    dbg!(Foo {
        /// the first
        a: 123,
        /// the second
        b: 456,
    });

like a: 123 aren't expressions, though the values (like the 123) you're assigning to the fields are. However, you can't move the comments to the expressions because you can only put attributes on certain kinds of expressions:

dbg!(Foo {
    a:
    /// the first
    123,
    b:
    /// the second
    456,
});
error[E0658]: attributes on expressions are experimental
  --> src/main.rs:22:9
   |
22 |         /// the first
   |         ^^^^^^^^^^^^^
   |
   = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information

error[E0658]: attributes on expressions are experimental
  --> src/main.rs:25:9
   |
25 |         /// the second
   |         ^^^^^^^^^^^^^^
   |
   = note: see issue #15701 <https://github.com/rust-lang/rust/issues/15701> for more information

Anyway, one use case for putting attributes on expressions is conditional compilation:

dbg!((
    /// the first
    #[cfg(target_os = "windows")]
    123,
    /// the second
    #[cfg(target_os = "linux")]
    456,
    /// some value that's always there
    789,
));

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.