Published on

Rust Journey (Part 1)

Authors

“The truth is that everyone is bored, and devotes himself to cultivating habits.” ― Albert Camus, The Plague

Since I'm slowly learning how to create apps related to NFTs (using Solana and Rust), the Android team also released a tutorial for it https://google.github.io/comprehensive-rust; I've decided to go deep down and extend my knowledge in Rust.

At first glance.

Intimidating. I learned to program using C/C++ when I was in high school. It looks familiar but more on steroids. It enforces that everything is clear and concise. No unused variables; if mutable, add mut or & if read-only. Correctly add the typings for the arguments and so on. Like Typescript++

It was hard, and I was expecting it since learning new things and wrapping it up to your brain was challenging.

First challenge

The first exercise is to transpose a specific array of numbers. So loops are needed to execute correctly. But it takes time to figure out how I can code it using Rust. But I manage to do it!

fn transpose(matrix: [[i32; 3]; 3]) -> [[i32; 3]; 3] {
    let mut new_matrix = matrix;
    let mut increment = 0;
    for _n in matrix {
        new_matrix[increment] = [
            matrix[0][increment],
            matrix[1][increment],
            matrix[2][increment]
        ];
        increment += 1;
    }
    return new_matrix
}

fn pretty_print(matrix: &[[i32; 3]; 3]) {
    for n in matrix {
        for x in n {
            print!("{x} ");
        }
         print!("\n");
    }
}

fn main() {
    let matrix = [
        [101, 102, 103], // <-- the comment makes rustfmt add a newline
        [201, 202, 203],
        [301, 302, 303],
    ];

    println!("matrix:");
    pretty_print(&matrix);

    let transposed = transpose(matrix);
    println!("transposed:");
    pretty_print(&transposed);
}

I will continue learning simple things and become more familiar with the syntax and workarounds that I only see in Rust.

Happy Learning!