Use the #[init] attribute on a method to define a specific initialization routine. The #[init] macro ensures that the method can only be called if the contract has not been initialized yet; it will panic if called a second time.
Note: Even with an #[init] method, your struct must still implement Default. To prevent accidental usage of the default state, you can implement Default to panic or use the #[derive(PanicOnDefault)] macro.
#[near]
impl StatusMessage {
#[init]
pub fn new(user: String, status: String) -> Self {
let mut res = Self::default();
res.records.insert(user, status);
res
}
}
// To prevent default initialization:
#[near(contract_state)]
#[derive(PanicOnDefault)]
pub struct StatusMessage {
records: HashMap<String, String>,
}