Learn Rust the Hard Bitcoin Way
This course is now available in video format with recorded coding sessions, more visual diagrams, quizzes and extra practice problems! And still free! Check it out at http://btcdemy.thinkific.com
Learn Rust and review Bitcoin fundamentals by building a command-line program.
We are going to build a relatively small command line program that decodes a raw transaction. Don't worry if you don't exactly know what that means, we'll review all the basic Bitcoin concepts and walk you through it step by step. If you are familiar with bitcoin-cli
, this is the equivalent of bitcoin-cli decoderawtransaction [raw transaction hex]
.
Ideally, you have some experience with programming and have read Mastering Bitcoin. This is not required, but it will make the learning process a lot smoother.
Rust is becoming very popular for new Bitcoin projects. Some examples of popular open source Bitcoin projects being built in Rust include Rust-Bitcoin, BDK, LDK, and Fedimint. But it's not just Bitcoin. For 8 years in a row, Rust topped the charts as the most desired programming language in Stack Overflow's annual developer survey (https://github.blog/2023-08-30-why-rust-is-the-most-admired-language-among-developers/). Why is that? Well, Rust allows developers to write systems-level programs in a way that is extremely performant, memory-safe, reliable and readable. It has great documentation, tooling, and a large and growing ecosystem. These are all very important features, especially for an important protocol like Bitcoin which will ultimately run the entire world's money system!
It's not as difficult as you might think. It just takes some time to get familiar with a new way of doing things. At first, it might feel more restrictive and tedious to write Rust code, but this is because the language makes certain tradeoffs that yield many long-term benefits in terms of performance, safety, effectiveness and readability. The compiler forces you to reason more carefully about your program and what it's actually doing under the hood. My hope is that by the end of this project you come to appreciate Rust and feel more confident writing Rust programs. I transitioned from Ruby and Javascript to Rust and I can't see myself ever going back!
By the end of this project, you will have understood the fundamental concepts of Rust such as types, data structures, references, stack and the heap, traits, error handling and more. We will also review some basic aspects of the Bitcoin protocol, specifically the components of a transaction. In future courses, we will dive deeper into the Bitcoin protocol and write more complex programs, exploring the full extent of Bitcoin's capabilities.
- 1.0: Project Overview
- 2.0: Setup
- 3.0: Our First Function
- 4.0: Hex and Bytes
- 5.0: Vectors and the Result Enum
- 6.0: Pointers and Slices
- 7.0: Arrays and Type Conversions
- 8.0: Traits and Reading Bytes
- 9.0: Mutable References
- 9.1: Shared References
- 10.0: CompactSize Unsigned Integers
- 11.0 Unit Testing
- 12.0 Reading Inputs and Type Coercion
- 13.0 Structs
- 14.0 JSON Serialization
- 15.0 Reading Outputs and Tuple Structs
- 16.0 Custom Serialization and Generic Functions
- 17.0 File Organization and Modules
- 18.0 Decoding a Legacy Transaction
- 19.0 Error Handling
- 20.0 Command Line Arguments
- 21.0 Refactoring and the Rust-Bitcoin Library
- 22.0 Decoding Segwit
- Quiz Solutions