View source code
Display the source code in core/attribute.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Enum core.attribute.mustuse

Use this attribute to ensure that values of a struct or union type are not discarded.

enum mustuse : void { ... }

The value of an expression is considered to be discarded if

  • the expression is the top-level expression in a statement or the left-hand expression in a comma expression, and
  • the expression is not an assignment (=, +=, etc.), increment (++), or decrement (--) expression.

If the declaration of a struct or union type has the @mustuse attribute, the compiler will emit an error any time a value of that type would be discarded.

Currently, @mustuse is only recognized by the compiler when attached to struct and union declarations. To allow for future expansion, attaching @mustuse to a class, interface, enum, or function declaration is currently forbidden, and will result in a compile-time error. All other uses of @mustuse are ignored.

Enum members



@mustuse struct ErrorCode { int value; }

extern(C) ErrorCode doSomething();

void main()
    // error: would discard a value of type ErrorCode

    ErrorCode result;
    // ok: value is assigned to a variable
    result = doSomething();

    // ok: can ignore the value explicitly with a cast
    cast(void) doSomething();


Jacob Carlborg


Boost License 1.0