โ† Back to list

Btrust ๐Ÿฆ€ for Bitcoiners: Week 2 โ€” It Gets Tougher

Week Two Task Reflection

Stephanie Nwankwo ยท 2025-06-27 02:26 ยท 0 claps ยท 4.6 min read
#rust-programming-language #bitcoin #btrust
Open on Medium โ†—
Wiki topics: CRY ยท Crypto & Web3 ๐Ÿ’ป ยท Programming

Btrust ๐Ÿฆ€ for Bitcoiners: Week 2 โ€” It Gets Tougher

Week Two Task Reflection

week two task email

week two task email

BTCdemy taught me a lot this past week. I had to do some extra research to really understand some of the concepts, but it was worth it. Hereโ€™s my personal take on Diving Deeper into Rust using the BTCdemy platforms.

1. Vectors

A vector is a way of representing a collection of values.

let mut vec = Vec::new();

But an easier and more common way is using the macro:

let mut vec = vec![1, 2];

I also learned how to convert a hex string into a vector. We used the hex crate (hex = "0.4"), and the decode() function helped us convert hex strings into vectors.

2. Enums

Enums allow us to describe a set of possible values (variants) for a given category, where only one variant is valid at a time. Each variant can even hold additional data.

To access a variant inside an enum, we often use match, and we must account for every possible variant. But when there are too many, and we don't want to match all of them, we can use the _ (underscore) as a catch-all for any remaining patterns.

Result Enum

The Result type represents either a success (Ok) or a failure (Err):

Ok(T) holds the successful value.

Err(E) holds the error value.

(Note: T and E are generic types โ€” they can be any data type.)

The .unwrap() method will return the value inside Ok, but beware โ€” if it's an Err, your program will panic. Speaking of panics, there's also the panic!() macro, like this:

panic!("Error: something went wrong");

But itโ€™s not user-friendly โ€” it crashes the program and displays an abrupt error message.

3. Pointers and Slices

All data is kept in memory (RAM) so it can be used and manipulated. In Rust, we deal with two main memory regions:

Stack

The stack is structured. It stores variables and functions in a last-in, first-out (LIFO) order โ€” like a stack of plates. Itโ€™s fast and efficient but limited in size.

Heap

The heap is unstructured and more flexible. It allows for dynamic memory allocation but is slower to access. When sharing large data between functions, using the heap makes it more efficient โ€” we simply use a pointer (reference) to where the data is stored.

4. Arrays

The Bitcoin protocol uses little endian format, which affects how arrays and bytes are stored and interpreted. Itโ€™s important when working with data encoding or decoding.

That means the least significant byte comes first. So if you see a hex string like 0x12345678, it would be stored as 78 56 34 12.

Weird at first, but itโ€™s a historical design choice. Rust makes it pretty easy to convert between big endian and little endian with built-in methods.

5. Traits

Traits in Rust are like interfaces โ€” they define shared behavior.

To use a trait, we often need to bring it into scope. For example:

use std::io::Read;

That Read trait gives types the ability to read bytes from a source. Traits are super useful when you want to write generic code that works on multiple types โ€” like saying "I donโ€™t care what type this is, as long as it can read!"

Itโ€™s one of those things Iโ€™ll probably appreciate more the deeper I go.

6. Mutable References

You canโ€™t have two mutable references to the same data at the same time. Rust makes sure of this at compile time to prevent bugs and data races.

This partโ€ฆ yeah. It took some thinking.

Rust is very strict about how data is shared or changed. And it makes sense โ€” itโ€™s trying to prevent bugs before they happen.

Here are the golden rules I noted:

  1. You can have one mutable reference, or multiple shared references, but not both.
  2. You canโ€™t have two mutable references at the same time.
  3. You canโ€™t read (&) and write (&mut) at the same time.

Why? So you donโ€™t end up in a situation where youโ€™re reading old data while itโ€™s being changed. Rust wants us to be intentional about every access to memory.

An example:

let mut vec = vec![1, 2, 3];

for x in &vec { vec.push(*x); // โŒ Nope! Youโ€™re reading and writing at the same time. }`

This will panic or fail to compile โ€” Rust says, โ€œNope. Try again.โ€

7. Ownership and References

This part really hit me โ€” understanding ownership is key in Rust. Ownership is one of Rustโ€™s biggest concepts. Every value has one owner โ€” and only one. You can borrow data by creating references (&value) or mutable references (&mut value), but you have to respect the borrowing rules.

Hereโ€™s the golden rule:

At any given time, you can have either:

i. One mutable reference, or

ii. Multiple shared references โ€” but not both.

You canโ€™t:

i. Have more than one mutable reference.

ii. Have a mutable and shared reference at the same time.

This ensures that you donโ€™t read data while itโ€™s being modified โ€” very helpful in avoiding bugs.

For example: You canโ€™t iterate over a vector and modify it at the same time.

This is all about keeping data safe and avoiding unexpected behavior.

  1. A dangling reference

A dangling reference happens when a reference points to memory that has been freed or is no longer valid. Rust prevents this entirely at compile time.

Thatโ€™s my summary for Week 2. It really gets tougher, but I love the challenge. Rust is strict, but in a way that protects you from yourself โ€” and I respect that.


๋ฉ”ํƒ€๋ฐ์ดํ„ฐ
post_id
b2596552df3a
slug
btrust-for-bitcoiners-week-2-b2596552df3a
url
https://medium.com/@iamtechhunter/btrust-for-bitcoiners-week-2-b2596552df3a
canonical_url
https://medium.com/@iamtechhunter/btrust-for-bitcoiners-week-2-b2596552df3a
author_url
https://medium.com/@iamtechhunter
status
ok
fetched_at
2026-07-19 06:43:54