In substrate pallets, build
is often used in decl_storage.
e.g.
pub ReferendumCount get(fn referendum_count) build(|_| 0 as ReferendumIndex): ReferendumIndex;
What does the build method do, how and when it is used?
From substrate.dev:
Whereas the config extension to the decl_storage macro allows you to configure a module's genesis storage state within a chain specification, the build extension allows you to perform this same task within the module itself (this gives you access to the module's private functions). Like config, the build extension accepts a single parameter, but in this case the parameter is always required and must be a closure, which is essentially a function. The build closure will be invoked with a single parameter whose type will be the pallet's GenesisConfig type (this gives you easy access to all the attributes of the GenesisConfig type). You may use the build extension along with the config extension for a single storage item; in this case, the pallet's GenesisConfig type will have an attribute that corresponds to what was set using config whose value will be set in the chain specification, but it will be the value returned by the build closure that will be used to set the storage item's genesis value.