> ## Documentation Index
> Fetch the complete documentation index at: https://mintlify.com/solana-foundation/anchor/llms.txt
> Use this file to discover all available pages before exploring further.

# Macros

> Reference for Anchor's Rust macros including

Anchor provides several powerful macros that reduce boilerplate and enforce security best practices.

## Program macros

### #\[program]

Defines the module containing instruction handlers.

```rust theme={null}
#[program]
pub mod my_program {
    use super::*;
    
    pub fn initialize(ctx: Context<Initialize>) -> Result<()> {
        // Instruction logic
        Ok(())
    }
}
```

### declare\_id!

Declares the program's on-chain address.

```rust theme={null}
declare_id!("Fg6PaFpoGXkYsidMpWTK6W2BeZ7FEfcYkg476zPFsLnS");
```

See [Program Structure](/concepts/program-structure) for more details.

## Account macros

### #\[derive(Accounts)]

Validates and deserializes accounts for an instruction.

```rust theme={null}
#[derive(Accounts)]
pub struct Initialize<'info> {
    #[account(init, payer = user, space = 8 + 8)]
    pub data: Account<'info, Data>,
    #[account(mut)]
    pub user: Signer<'info>,
    pub system_program: Program<'info, System>,
}
```

### #\[account]

Creates a custom account type with automatic serialization.

```rust theme={null}
#[account]
pub struct Data {
    pub value: u64,
}
```

## Event macros

### #\[event]

Defines an event that can be emitted.

```rust theme={null}
#[event]
pub struct DataUpdated {
    pub old_value: u64,
    pub new_value: u64,
}
```

### emit!

Emits an event from your program.

```rust theme={null}
emit!(DataUpdated {
    old_value: 0,
    new_value: 42,
});
```

## Error macros

### #\[error\_code]

Defines custom error codes.

```rust theme={null}
#[error_code]
pub enum ErrorCode {
    #[msg("Invalid authority")]
    InvalidAuthority,
    #[msg("Insufficient funds")]
    InsufficientFunds,
}
```

See [Errors](/guides/errors) for more details.

## Constraint macros

### require!

Assert a condition is true.

```rust theme={null}
require!(ctx.accounts.user.key() == ctx.accounts.data.authority, ErrorCode::InvalidAuthority);
```

### require\_keys\_eq!

Assert two public keys are equal.

```rust theme={null}
require_keys_eq!(ctx.accounts.user.key(), ctx.accounts.data.authority);
```

See [Account Constraints](/api/rust/account-constraints) for all constraints.
