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?
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?
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.
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.
Under the hood, anchor build
does cargo build-bpf
and then extracts the program's IDL at src/lib.rs
.
cargo build-bpf
(now cargo build-sbf
) differs from cargo build
because it specifically builds a Solana on-chain program, and not a general binary / library that can be used on your system.
And the IDL is an important feature of Anchor -- it exposes the program's interface to be consumed by any client.