Kick-Start Solana Programming with Anchor

·

5 min read

Kick-Start Solana Programming with Anchor

Jumping into Solana programming can seem somewhat daunting. These tips and pointers should help you along with your exciting new chapter.

Local Development

The book already calls out the required software for building Anchor programs.

I highly recommend Linux, macOS or Windows Subsystem for Linux v2 (WSL2) as the development environments.

For IDE, Visual Studio Code provides the optimal development experience at the best price (free!). Here are my suggestions for VS Code extensions to install:

  • rust-analyzer: full support for Rust programming. I would avoid the RLS extension as it has been deprecated in favour of the rust-analyzer and has not received an update in a few years.
  • CodeLLDB: for debugging Rust programs. For those brave souls with a pure (non-WSL) Windows development environment, you may want to look at the C/C++ extension instead.

TypeScript support comes out of the box. Some other nice-to-have extensions include Better TOML, Prettier and Remote - WSL (mandatory if working on WSL2).

I should note if you are more comfortable with the JetBrains extended universe, Rust support can be installed into your IDE as well. However, to enable Rust debugging, you will need CLion or other paid variants with Native Debugging Support.

If you are after an IntelliJ IDE that has both Rust and TypeScript support, you are limited to the paid IntelliJ IDEA Ultimate.

VS Code Workspace for Mono-Repos

Anchor projects are made up of a Cargo workspace, as specified in the root Cargo.toml file, and one or more programs in their own Cargo crates, as specified in programs/*/Cargo.toml.

rust-analyzer currently only supports one Cargo workspace per VS Code workspace by default. You may face errors like the below if you open a mono-repo with multiple Anchor projects in it.

[ERROR rust_analyzer] failed to find any projects in [AbsPathBuf("...")]
[ERROR rust_analyzer::lsp_utils] rust-analyzer failed to discover workspace

The recommended way to get around this is to turn off the project auto-discovery of the plugin and specify each Anchor project manually. This can be done in .vscode/settings.json of your VS Code workspace:

{
  "rust-analyzer.linkedProjects": [
    "an_anchor_project/Cargo.toml", 
    "another_anchor_project/Cargo.toml"
  ]
}

Learning Rust

Rust programming can be one of the bigger hurdles when moving into the Solana ecosystem, especially if you do not have a system programming background.

Everyone learns differently so I cannot possibly recommend one efficient way to get upskilled. But here are some tips:

  • For those that learn by reading, the Rust book is indispensable.
  • For those that learn by doing, Rust by Example can be the way to go.
  • For those that learn by watching, you are spoiled for choice: there are plenty of YouTube videos and courses on educational sites, such as Udemy.

I personally find the first two options more effective. As someone who knows a few programming languages already, I can skip and skim through chapters and only learn what I do not already know.

Regardless of how you learn, you do not need to learn every aspect of Rust to be a Solana developer (though I highly recommend learning as much as you can - Rust is a great language!). Here are some topics to focus on:

  • Basics of the language: variables, data types, functions, control flow, etc.
  • Basics of cargo: crates, modules, command line, etc.
  • Ownership, references and borrowing
  • Structs and implementations
  • Common collections, e.g. vectors
  • Enums and pattern matching
  • Error handling
  • Generic types, traits and lifetimes
  • Smart pointers, especially Box<>
  • Basics of Macros

Here are some topics that are either not applicable or you will not need at the beginner or even intermediate level:

  • I/O: filesystem or network
  • Fearless concurrency: threads, process, futures, sync/send, etc.
  • Developing macros
  • OS-specific libraries
  • Unsafe Rust

Learning Anchor

Anchor documentation is evolving rapidly and is actively being updated. Please keep that in mind when reading this section.

Anchor framework is a good on-ramp onto Solana development since it provides a few safety nets and ergonomics that you may miss otherwise.

A quick search will bring up a raft of tutorials written by the community. Most of them cover the basics well. The only thing to watch out for is the date when the tutorial was published. Some of the older ones may no longer be valid.

The Anchor book itself is a good starting place that covers topics in a mix of theory and practice. It is missing some topics and could use a few runs of edit but otherwise, it is great.

I personally learn more from fully working examples so Anchor GitHub repo is the place to spend time in:

The tests are set up differently than normal Anchor projects. If you want to run them locally, consult the repo's GitHub Actions for hints.

So far, we haven't spoken much about learning Solana itself. There is a lot to learn there, that will require its own post. But in the meanwhile, I would at the minimum recommend getting your head around the basics via either their official docs or the cookbook.

Community

As is the case with any other project in this space, the community is motivated for the projects to succeed so there is plenty of support available.

Anchor Discord is quite active and both users and contributors are more than happy to help each other out. Following Anchor on Twitter can also be worthwhile to keep on top of the latest updates.

Solana has recently achieved sufficient commitment for a StackOverflow site so hopefully soon, there will also be a specialised Q&A site to make community support even smoother!