4

This is an example of account struct in an anchor project.

#[derive(Accounts)]
pub struct Create<'info>{
    #[account(init,payer=user,space=264)]
    pub calculator:Account<'info,Calculator>,
    #[account(mut)]
    pub user:Signer<'info>,
    pub system_program:Program<'info,System>
}

As far as I know, generic lifetime parameters are only needed when we use references as output from a function. so why so we need a lifetime parameter for an account struct

1 Answer 1

2

In a Solana program, accounts and their data are provided to a program, so the program can read / modify the data as it wants. The Anchor Account type wraps the solana_program::account_info::AccountInfo type, which is itself a wrapper over Rust references.

Since AccountInfo holds references, those references must have lifetimes so that the Rust borrow checker can verify their usage. By extension, Anchor Accounts must also have lifetimes associated.

You can find more info about AccountInfo at https://github.com/solana-labs/solana/blob/d351c58ff478bf4c9500fd1a2451b52307f668f6/sdk/program/src/account_info.rs#L18

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.