Attributes
An attribute is written @name above a declaration, optionally with a string argument. Attributes are a closed set: the compiler knows every one it accepts and rejects anything else with an error. A misspelled attribute can therefore never be silently ignored, which is the failure mode that makes attributes untrustworthy in other languages — @tset is a compile error, not a test that quietly never runs.
use system.io
// @non_exhaustive: this enum may gain variants later, so a match on it
// outside the defining module must carry a default arm.
@non_exhaustive
public enum ConnectionError
Refused(String)
TimedOut(int)
// @must_use: the value may not be silently discarded at a call site.
@must_use
public enum Receipt
Written(int)
// @deprecated carries the replacement, and callers get a warning.
@deprecated("acquire a Clock and use clock.now()")
fn legacy_timestamp() int
0
fn main()
println("attributes are a closed set — an unknown one is a compile error")
| Attribute | Applies to | Argument | Effect |
|---|---|---|---|
@non_exhaustive | Enum | — | The enum may gain variants, so a match on it outside the defining module needs a default arm. |
@must_use | Enum | — | The value may not be silently discarded at a call site. |
@deprecated | Function, class, enum | Required | Callers get a warning carrying the message — normally the replacement to use. |
@test | Function | — | Marks the function for discovery by miri test. |
@ignore | Function | Required | Skips the test and reports the reason. Requires @test. |
@xfail | Function | Required | The test is expected to fail. Requires @test. |
Applying an attribute to the wrong kind of declaration, omitting a required argument, or passing one where none is accepted are all errors as well — as is using @ignore or @xfail without the @test they depend on.