Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Annotations

optional

The @optional annotation will cause member fields to be wrapped in an Option<T> type.

default and default_literal

The @default and @default_literal annotations affect the values generated in the new() function.

external

The @external annotation will cause the value to be wrapped in a Box<T>. This indicates that the value is allocated on the heap and owned through a pointer indirection.

const and static

Function prototypes may be annotated with @const or @static.

derive

The @derive annotation adds custom derive macros to generated types.

Rust offers access to the syntax tree during compilation through derive macros. In some cases, it may be desirable by the user to expand upon the generated types through such macros. To allow for this, the @derive annotation adds user-specified derive macros to the attached type.

For example:

@derive("MyDerive")
struct MyStruct {};

Results in the following Rust code:

#![allow(unused)]
fn main() {
#[derive(..., MyDerive)]
struct MyStruct {};
}

Rust does not require the relevant symbol to be explicitly imported. Thus, the user will have to be responsible for providing a qualified name to the macro.