<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Ann Maina</title>
    <description>The latest articles on Forem by Ann Maina (@nyagooh).</description>
    <link>https://forem.com/nyagooh</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2425824%2Fe245f2c7-18ad-4818-b094-1666111fa3f3.JPG</url>
      <title>Forem: Ann Maina</title>
      <link>https://forem.com/nyagooh</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/nyagooh"/>
    <language>en</language>
    <item>
      <title>🚀 Mastering Vectors in Rust</title>
      <dc:creator>Ann Maina</dc:creator>
      <pubDate>Thu, 26 Jun 2025 18:23:02 +0000</pubDate>
      <link>https://forem.com/nyagooh/mastering-vectors-in-rust-1m0h</link>
      <guid>https://forem.com/nyagooh/mastering-vectors-in-rust-1m0h</guid>
      <description>&lt;p&gt;In Session 6 of the Polkadot Kisumu Bootcamp, we dived deep into one of the most practical and powerful data structures in Rust: Vectors.&lt;/p&gt;

&lt;p&gt;If you’ve ever needed to store a list of items that can change size, vectors are your best friend in Rust.&lt;/p&gt;

&lt;h2&gt;
  
  
  What Is a Vector?
&lt;/h2&gt;

&lt;p&gt;A vector is a resizable array that stores values on the heap. It's useful when you don’t know the size of your data at compile time.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let mut numbers = Vec::new();
    numbers.push(10);
    numbers.push(20);
    println!("{:?}", numbers); // Output: [10, 20]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🔄 Using .push() and .pop()
&lt;/h2&gt;

&lt;p&gt;These are the basic ways to grow or shrink a vector.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let mut fruits = vec!["apple", "banana"];
    fruits.push("mango");  // Add item
    fruits.pop();          // Remove last item
    println!("{:?}", fruits); // Output: ["apple", "banana"]
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  ✂️ Slices – Borrowing Part of a Vector
&lt;/h2&gt;

&lt;p&gt;Slices let you access a section of the vector without taking ownership.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let data = vec![1, 2, 3, 4, 5];
    let slice = &amp;amp;data[1..4];
    println!("{:?}", slice); // Output: [2, 3, 4]
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is memory-efficient and avoids unnecessary copying.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧬 Generics – Writing Flexible Code
&lt;/h2&gt;

&lt;p&gt;Generics in Rust let you write functions or types that work with any data type, as long as the type meets certain conditions.&lt;/p&gt;

&lt;p&gt;Instead of writing duplicate code for each type (i32, String, etc.), you can use a generic type parameter like T.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn print_all&amp;lt;T: std::fmt::Debug&amp;gt;(items: Vec&amp;lt;T&amp;gt;) {
    for item in items {
        println!("{:?}", item);
    }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What's Happening:&lt;/p&gt;

&lt;p&gt;T is a placeholder for any type.&lt;/p&gt;

&lt;p&gt;T: std::fmt::Debug means the type T must implement the Debug trait so it can be printed.&lt;/p&gt;

&lt;p&gt;You can now call print_all() with Vec, Vec&amp;lt;&amp;amp;str&amp;gt;, Vec, etc.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn main() {
    let numbers = vec![1, 2, 3];
    let names = vec!["Ann", "Ben", "Chris"];

    print_all(numbers);
    print_all(names);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;🧠 Generics = less repetition + more flexibility.&lt;br&gt;
This is the Rust way to write clean, DRY (Don't Repeat Yourself), scalable code.&lt;/p&gt;
&lt;h2&gt;
  
  
  🔁 Iteration Methods – Understanding Ownership in Loops
&lt;/h2&gt;
&lt;h2&gt;
  
  
  .iter() – Immutable Borrow
&lt;/h2&gt;

&lt;p&gt;You borrow each item without modifying or taking ownership.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nums = vec![1, 2, 3];
for n in nums.iter() {
    println!("{}", n); // Can read, but not modify
}
// nums is still usable here

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when you just want to read from the vector.&lt;/p&gt;

&lt;h2&gt;
  
  
  .iter_mut() – Mutable Borrow
&lt;/h2&gt;

&lt;p&gt;You borrow each item mutably, which means you can change the values.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let mut nums = vec![1, 2, 3];
for n in nums.iter_mut() {
    *n *= 2; // Modify in-place
}
println!("{:?}", nums); // Output: [2, 4, 6]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Use this when you need to edit values in the vector.&lt;/p&gt;

&lt;h2&gt;
  
  
  .into_iter() – Take Ownership
&lt;/h2&gt;

&lt;p&gt;You move each item out of the vector — meaning the original vector is no longer usable after the loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let nums = vec![1, 2, 3];
for n in nums.into_iter() {
    println!("{}", n);
}
// nums is now invalid — ownership moved

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Use this when you're done with the vector and want to consume it completely.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why This Matters
&lt;/h2&gt;

&lt;p&gt;Understanding generics and iteration methods gives you real power in Rust:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; You write once, reuse everywhere (generics).&lt;/li&gt;
&lt;li&gt; You control how memory is accessed, modified, or moved (iterators).&lt;/li&gt;
&lt;li&gt; It ties directly back to ownership and borrowing, which are core to writing safe, high-performance Rust.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This builds on earlier sessions like ownership and borrowing making your code more safe, clean, and production-ready.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>web3</category>
    </item>
    <item>
      <title>🦀 Understanding Lifetimes, Traits, Enums, and Pattern Matching in Rust</title>
      <dc:creator>Ann Maina</dc:creator>
      <pubDate>Mon, 23 Jun 2025 18:42:20 +0000</pubDate>
      <link>https://forem.com/nyagooh/understanding-lifetimes-traits-enums-and-pattern-matching-in-rust-2dc7</link>
      <guid>https://forem.com/nyagooh/understanding-lifetimes-traits-enums-and-pattern-matching-in-rust-2dc7</guid>
      <description>&lt;p&gt;Rust is known for being safe and fast but to get there, you need to understand some core concepts. Let’s break down lifetimes, traits, enums, and pattern matching with clear examples.&lt;/p&gt;

&lt;h2&gt;
  
  
  🔗 Lifetimes – Keeping References Safe
&lt;/h2&gt;

&lt;p&gt;Rust makes sure you don’t use data that’s already gone. Lifetimes are how Rust tracks how long references are valid.&lt;br&gt;
🧠 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;fn longest&amp;lt;'a&amp;gt;(x: &amp;amp;'a str, y: &amp;amp;'a str) -&amp;gt; &amp;amp;'a str {
    if x.len() &amp;gt; y.len() { x } else { y }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This function returns the longer of two string slices. The &amp;lt;'a&amp;gt; tells Rust:&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"The result reference will live as long as both x and y."
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;

&lt;p&gt;✅ Without this, Rust won’t know how long the returned reference lives  and could reject your code to keep things safe.&lt;/p&gt;

&lt;h2&gt;
  
  
  🧰 Traits – Defining Shared Behavior
&lt;/h2&gt;

&lt;p&gt;Traits define what methods a type must implement. Think of them like "skills" a type can have.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
trait Speak {
    fn speak(&amp;amp;self);
}

struct Dog;

impl Speak for Dog {
    fn speak(&amp;amp;self) {
        println!("Woof!");
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now any type that implements Speak must have a speak() method. You can call it like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
let d = Dog;
d.speak(); // Outputs: Woof!
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ Traits help you write reusable and generic code.&lt;/p&gt;

&lt;h2&gt;
  
  
  🎭 Enums – One Type, Many Forms
&lt;/h2&gt;

&lt;p&gt;Enums allow a value to be one of several predefined options and each can hold different data.&lt;br&gt;
🧠 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;enum Status {
    Success,
    Error(String),
    Loading,
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This enum can represent three app states. You can use it like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let response = Status::Error(String::from("Something went wrong"));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  🧩 Pattern Matching – Handling Enums Safely
&lt;/h2&gt;

&lt;p&gt;With enums, you use match to handle each possible value.&lt;br&gt;
🧠 Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;match response {
    Status::Success =&amp;gt; println!("All good!"),
    Status::Error(msg) =&amp;gt; println!("Error: {}", msg),
    Status::Loading =&amp;gt; println!("Loading..."),
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;✅ This forces you to handle every case, making your code safer and easier to understand.&lt;br&gt;
🧠 Wrap Up&lt;/p&gt;

&lt;p&gt;Here’s what you’ve learned:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;   Lifetimes: Help Rust ensure references don’t outlive the data they point to.&lt;/li&gt;
&lt;li&gt;    Traits: Let you define shared behavior that different types can implement.&lt;/li&gt;
&lt;li&gt;    Enums: Represent values that can take different forms.&lt;/li&gt;
&lt;li&gt;    Pattern matching: Makes handling all possible values safe and clear.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rust might seem strict at first, but that’s what makes your programs fast and bug-free.&lt;br&gt;
Keep practicing, and these concepts will become second nature. &lt;/p&gt;

</description>
      <category>rust</category>
      <category>lifetimes</category>
      <category>development</category>
    </item>
    <item>
      <title>🦀 Understanding Ownership in Rust: A Dev's Guide to Safe Memory Management</title>
      <dc:creator>Ann Maina</dc:creator>
      <pubDate>Wed, 18 Jun 2025 07:29:18 +0000</pubDate>
      <link>https://forem.com/nyagooh/understanding-ownership-in-rust-a-devs-guide-to-safe-memory-management-51o5</link>
      <guid>https://forem.com/nyagooh/understanding-ownership-in-rust-a-devs-guide-to-safe-memory-management-51o5</guid>
      <description>&lt;p&gt;Rust is known for its blazing-fast performance without a garbage collector and at the heart of that magic lies ownership. For devs coming from languages like Python, JavaScript, or even C++, Rust’s ownership model might feel a little strict at first. But once you get the hang of it, it becomes your best ally in writing safe, concurrent code without worrying about memory leaks or race conditions.&lt;/p&gt;

&lt;p&gt;Let’s break it down with real-world analogies and simple concepts.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Ownership: One Value, One Boss
&lt;/h2&gt;

&lt;p&gt;In Rust, every value has a single owner—a variable that holds responsibility for it. When you assign that value to another variable, the ownership moves. The original variable is no longer valid.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`let book = String::from("Rust Book");
let borrowed = book; // book is moved here
// println!("{}", book); ❌ Error: book no longer owns the value`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Analogy: Think of it like handing over a physical book. Once you give it away, you can't read it anymore unless it's given back.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Move Semantics: Handing Over Ownership
&lt;/h2&gt;

&lt;p&gt;Rust uses move semantics by default for types like String or Vec. When you assign or pass these values around, ownership is transferred—not copied.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
`fn print_book(title: String) {
    println!("{}", title);
}

let title = String::from("Rust Ownership");
print_book(title); // title is moved here
// println!("{}", title); ❌ Won't compile`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This ensures that there's only one owner at any given time—no double frees, no use-after-free bugs.&lt;/p&gt;

&lt;h2&gt;
  
  
  📖 3. Immutable Borrowing: Sharing Without Risk
&lt;/h2&gt;

&lt;p&gt;Sometimes, you want to let others look at your data without changing it. Rust allows this with immutable references:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`
let title = String::from("Borrow Me");
let ref1 = &amp;amp;title;
let ref2 = &amp;amp;title; // ✅ multiple reads allowed
println!("{}, {}", ref1, ref2);`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Analogy: Like sharing your notes in class you’re letting others read, but they can’t mark or edit them.&lt;/p&gt;

&lt;h2&gt;
  
  
  ✏️ 4. Mutable Borrowing: One Writer Only
&lt;/h2&gt;

&lt;p&gt;If you want to allow edits, Rust ensures only one mutable reference exists at a time. This prevents conflicts and keeps your data consistent.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`let mut title = String::from("Update Me");
let ref_mut = &amp;amp;mut title;
ref_mut.push_str(" Now!");
// let ref_mut2 = &amp;amp;mut title; ❌ Only one mutable ref at a time`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Why this matters: It avoids data races, where multiple threads (or code paths) might try to change the same data simultaneously.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. Scope &amp;amp; Auto Memory Management
&lt;/h2&gt;

&lt;p&gt;Rust doesn't need a garbage collector. Instead, when a value goes out of scope, Rust automatically drops it freeing the memory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;{
    let title = String::from("Temp Book");
} // title is dropped here 🧹`
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No more free() calls, no memory leaks Rust handles it all, compile-time.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts: Ownership Makes You Think Differently
&lt;/h2&gt;

&lt;p&gt;Rust’s ownership model forces you to be explicit and intentional about how you use memory. It may slow you down at first, but it pays back in:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;    Fewer bugs&lt;/li&gt;
&lt;li&gt;    Safer concurrency&lt;/li&gt;
&lt;li&gt;    Zero-cost abstractions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And the best part? Once it compiles, it usually just works.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>web3</category>
    </item>
  </channel>
</rss>
