1

I follow hackernoon-solana to see the syntax in non anchor project. this is the function signature

pub fn process_instruction(program_id:&Pubkey,accounts:&[AccountInfo],input:&[u8])->ProgramResult{}

we pass accounts:&[AccountInfo] array as an arg. Args are always byte array

// Iterating accounts is safer then indexing
let accounts_iter = &mut accounts.iter();

Then I get the first account

let account = next_account_info(accounts_iter)?;

then deserialize the passed arg. in rust arguments are an array of bytes

 let mut greeting_account = GreetingAccount::try_from_slice(&account.data.borrow())?;

So far so good. but I did not understand this line:

 greeting_account.serialize(&mut &mut account.data.borrow_mut()[..])?;

We are storing the data inside greeting_account. BUt what is this [..] for? account is a single object, how can we use a range operator on an object?

1 Answer 1

2

That's just another way of dereferencing to get the internal data in Rust. &mut account.data.borrow_mut()[..] and *account_info.data.borrow_mut() should get the same result. You could write that line as:

greeting_account.serialize(&mut *account.data.borrow_mut())?;
1
  • This is correct, but in general, please use the to_writer helper to avoid issues trashing the underlying slice. See github.com/near/borsh-rs/pull/34 for more info
    – Jon C
    Commented Jan 20, 2023 at 18:09

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.