11

I see that most programs put everything in a lib.rs file. My file is more than 500 lines so I feel like it should be multiple files.

I have seen people using a file error.rs for errors so I have done that.

Is there guidelines or good practices to continue splitting my big program ?

2 Answers 2

10

Bigger projects typically use the multiple files template added in Anchor 0.29. You can do this for new projects with:

anchor init counter --template=multiple

This splits up state/ and instructions/. Under these folders, you can create files for each account type and instruction handler.

For example, a simple counter program might look like:

src/
  instructions/
    decrement.rs
    increment.rs
    initialize.rs
    mod.rs
  state/
    counter.rs
    mod.rs
  lib.rs
  errors.rs
Cargo.toml
README.md
3

A single lib.rs file can be good enough, even for 500-line code. There are some Solana programs with thousands of lines that use this format. It is very debatable which method is considered best though. It all depends on preference.

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.