Search code examples
rustrust-cargosolanaanchor-solanasolana-cli

Difference between "cargo build" and "anchor build"


I am following rust tutorials online, and I found that some websites are using cargo build command while others are using anchor build command to build the project.

What is the difference between these two commands?


Solution

  • Cargo is Rust's build manager.

    Anchor is a framework specifically for solana/rust. It has extra features for a better development experience. It is similar to truffle framework for Ethereum.

    What Anchor does for you

    Building Solana programs in Rust language can be tough. Anytime you want to save or retrieve data from an account, you need to think about packing/unpacking the data, serializing/unserializing the data and formats, which are all a real pain.

    Anchor abstracts away from the low level construction of accounts, the packing and unpacking, and modifying the interfaces to your Solana program. It does this by bundling the boilerplate into rust macros! This gives users of Anchor a lot of shortcuts and speed to building Solana programs. There are always two parts to a Solana app -- the on-chain program and accounts, and the off-chain app that interacts with it.

    The other benefit Anchor brings is the alignments of the interaction between these two app segments. This alignment is called an Interface Description Language (IDL). Since the interface of the off-chain app (say, in JavaScript) must always match the on-chain Solana program, this is a really nice convenience feature to have.