The official documentation shows this about the Mapping's initialization:

enter image description here

Is it fine if I initialize a Mapping using default? e.g.

initialize_contract(|contract: &mut Self| {
   contract.my_mapping = <Mapping<T, V>>::default();

In my case, V is a custom struct and the goal is to have an empty mapping after the contract initialization.


1 Answer 1


It's fine to initialize an empty Mapping. You can do this by not passing anything to the initialization closure.

The docs you linked to cover this default case:

pub fn default() -> Self {
  // Even though we're not explicitly initializing the `Mapping`,
  // we still need to call this
  ink_lang::utils::initialize_contract(|_| {})
  • The docs mention here use.ink/ink-vs-solidity/#mapping-declaration mention that when using a map in ink! we must to use ink_lang::utils::initialize_contract in the constructor and provides a link to use.ink/datastructures/mapping for more details, but that link doesn't actually show any further details about using initialize_contract, it simply shows an example of initialising the contract with an empty mapping in the constructor let balances = Mapping::default();. Jul 17, 2023 at 8:34
  • So do we still need to use ink_lang::utils::initialize_contract(|_| {}) like you've shown above to cover the fn default() case? Or do we just explicitly set variables to mapping defaults with Mapping::default() in fn new() instead now? Jul 17, 2023 at 8:34
  • It seems we still need to use ink_lang::utils::initialize_contract(|_| {}) since it is shown in an example here use.ink/ink-vs-solidity/… Jul 17, 2023 at 9:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.