init_if_needed (Anchor Constraint)

Dev Tools · Deep

An Anchor account constraint that creates and initializes an account only if it does not already exist, skipping initialization if the account is already present. Useful for idempotent operations like associated token account creation. Requires the init-if-needed feature flag in Cargo.toml. Must be used carefully — without a proper discriminator check, it can mask reinitialization vulnerabilities if the account exists but contains unexpected data.

Related terms