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.
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.
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.
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.
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?"
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,
));