@@ -65,36 +65,73 @@ Navigate to the new project directory and open it in your code editor.
6565cd my-project
6666```
6767
68- The default Anchor program is located at ` /programs/my-project/src/lib.rs ` .
68+ By default, Anchor generates a modular program structure to promote better code
69+ organization and maintainability. The program files are organized as follows:
70+
71+ - ` /programs/my-project/src/lib.rs ` - Main entry point with module declarations
72+ - ` /programs/my-project/src/instructions/ ` - Instruction handlers
73+ - ` /programs/my-project/src/state/ ` - Account structures and state
74+ - ` /programs/my-project/src/constants.rs ` - Program constants
75+ - ` /programs/my-project/src/error.rs ` - Custom error definitions
6976
7077<Accordions >
71- <Accordion title = " Default Program" >
78+ <Accordion title = " Default Program Structure " >
7279
7380The value in the ` declare_id! ` macro is the program ID, a unique identifier for
7481your program.
7582
7683By default, it is the public key of the keypair generated in
7784` /target/deploy/my_project-keypair.json ` .
7885
86+ ** Main entry point** (` lib.rs ` ):
87+
7988``` rust filename="lib.rs"
89+ pub mod constants ;
90+ pub mod error ;
91+ pub mod instructions ;
92+ pub mod state ;
93+
8094use anchor_lang :: prelude :: * ;
8195
96+ pub use constants :: * ;
97+ pub use instructions :: * ;
98+ pub use state :: * ;
99+
82100declare_id! (" 3ynNB373Q3VAzKp7m4x238po36hjAGFXFJB4ybN2iTyg" );
83101
84102#[program]
85103pub mod my_project {
86104 use super :: * ;
87105
88106 pub fn initialize (ctx : Context <Initialize >) -> Result <()> {
89- msg! (" Greetings from: {:?}" , ctx . program_id);
90- Ok (())
107+ initialize :: handler (ctx )
91108 }
92109}
110+ ```
111+
112+ ** Instruction handler** (` instructions/initialize.rs ` ):
113+
114+ ``` rust filename="instructions/initialize.rs"
115+ use anchor_lang :: prelude :: * ;
93116
94117#[derive(Accounts )]
95118pub struct Initialize {}
119+
120+ pub fn handler (ctx : Context <Initialize >) -> Result <()> {
121+ msg! (" Greetings from: {:?}" , ctx . program_id);
122+ Ok (())
123+ }
96124```
97125
126+ <Callout type = " info" >
127+
128+ For simpler projects or quick prototyping, you can use the single-file template
129+ with ` anchor init --template single ` . However, the modular structure is
130+ recommended for production code as it improves code organization, readability,
131+ and maintainability.
132+
133+ </Callout >
134+
98135</Accordion >
99136</Accordions >
100137
@@ -356,9 +393,17 @@ Below is an overview of default file structure in an Anchor workspace:
356393 <Folder name = " [project-name]" defaultOpen = { true } >
357394 <Folder name = " src" defaultOpen = { true } >
358395 <File name = " lib.rs" />
359- <File name = " Cargo.toml" />
360- <File name = " Xargo.toml" />
396+ <File name = " constants.rs" />
397+ <File name = " error.rs" />
398+ <Folder name = " instructions" defaultOpen = { true } >
399+ <File name = " mod.rs" />
400+ <File name = " initialize.rs" />
401+ </Folder >
402+ <Folder name = " state" >
403+ <File name = " mod.rs" />
404+ </Folder >
361405 </Folder >
406+ <File name = " Cargo.toml" />
362407 </Folder >
363408 </Folder >
364409 <Folder name = " target" defaultOpen = { true } >
@@ -387,6 +432,17 @@ Below is an overview of default file structure in an Anchor workspace:
387432The `/programs` directory contains your project's Anchor programs. A single
388433workspace can contain multiple programs.
389434
435+ By default, programs are organized with a modular structure:
436+
437+ - `lib.rs` - Main entry point that declares and exports modules
438+ - `instructions/` - Directory containing instruction handler functions
439+ - `state/` - Directory for account structures and state definitions
440+ - `constants.rs` - Program-wide constants
441+ - `error.rs` - Custom error codes
442+
443+ This modular organization makes it easier to navigate and maintain your code,
444+ especially as your program grows in complexity.
445+
390446### Tests Folder
391447
392448The `/tests` directory contains test files for your project. A default test file
0 commit comments