Rust Never Sleeps

The Rust language logo, courtesy of wikimedia.

Go brought me to a shuddering stop. I think I may have had some environment issues because I had some early install problems. These seem to be tricky to fix, so I may come back to Go later. Meanwhile, I've been trying Rust, learning from "The Rust Programming Language", AKA "The Rust Book." So far, the experience has been as easy as Arduino and C/C++ was when I started that journey 8 years ago. Challenging, but steady progress. And in sorting out a problem with my Rust installation, I may have hit upon the solution to my Go installation issues, too. Looks like Homebrew language installs might be a little problematic. Maybe my Homebrew install is buggered. Will come back to that, too, but after my Rust learning.

So, I'm as far as Chapter 2, "Programming A Guessing Game," where the reader is guided through constructing a command line guessing game. It has been a surprisingly easy experience. Rust has a syntax like C but feels... "easier." And also, potentially with traps, because, while adding an if/else, I bracketed the test function. That, while not insurmountable, is a trap I have to not set myself.

So, this is filling me with more confidence that I can learn another coding language, giving me a better understanding of Slint, as that framework is written in Rust. Although Slint is compatible with C++, and has given me some success in creating a simple GUI app, it's Rust native and carries a syntax feel like Rust. Fingers crossed then.

Meanwhile, using Rust in the command line, I was able to extend the Chapter 2 tutorial to include a turns count, winging what I suspected the Rust equivalent of a C if/else loop was, tripped, understood the compiler error and fixed it. Here's my version of the chapter 2 code from "The Rust Programming Language"...

// Rust
use std::io;
use rand::Rng;
use std::cmp::Ordering;

fn main() {
    println!("\n-------------------------");
    println!("Guess the number!");
    println!("-------------------------");

    let secret_number: u32 = rand::thread_rng().gen_range(1..=100);
    let mut score: i32 = 0;		// <-- my score counter variable

    // println!("The secret number is: {secret_number}"); // <-- test line, comment out for release

    loop {
        score = score + 1;		// <-- increment my score variable by 1
        println!("Please input your guess: ");
    
        let mut guess: String = String::new();

        io::stdin()
            .read_line(&mut guess)
            .expect("Failed to read line");

        let guess: u32 = match guess.trim().parse(){
            Ok(num) => num,
            Err(_) => continue,
        };

        print!("You guessed: {guess}");

        match guess.cmp(&secret_number) {
            Ordering::Less => println!(", that is too small!"),
            Ordering::Greater => println!(", that is too big!"),
            Ordering::Equal => {
            	// my modified exit code for the case of a correct guess
                if score == 1 {
                    println!(", you win in {score} move.");	// if score = 1
                } else {
                    println!(", you win in {score} moves.");	// score in all other cases
                }	//	end my modification
                break;
            }
        }
    }
    println!("-------------------------\n");
}

Yeah, like I say, Go seemed a bit of a struggle with sorting out my install, and I will return to that, but for what I want to acheive now, Rust has so far been the simplest language to learn since I first ran Basic from a remote terminal on a PDP-11/70 system in late high school. (1978)

Bonus: I'm now up to numerical iteration with for loops, via item iterised for loops and while loops. I've nearly caught up to my moderate C/C++ skills! :-D

Comments

Post a Comment

Popular posts from this blog

Removing Ollama From My Mac

Meanwhile, Developing a MIDI, Tap-Tempo, Master-Clock Pedal...

ALWAYS read the PDS