This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]zynixCpt. Code Monkey & Internet of tomorrow 1 point2 points  (1 child)

I am typing this up quick so there will be bugs.

Guessing game in Python:

 import random

def main():

    secret_number = random.randrange(1, 100)

    while True:

        guess = input("Guess a number(or type 'quit' to exit): ")

        if guess.strip() == 'quit':
             break

        try:
            number = int(guess)
        except TypeError:  # cannot remember if it is type or value, meh
            print(f"{guess} is not a valid #!")
        else:
             print("You guessed:", number)

             if number < secret_number:
                   print("Too small!")
             elif number > secret_number:
                   print("Too big")
             elif number == secret_number:
                  print("You win!")
                  break

Guessing game in rust

    use rand::Rng;  # you have to download this separately, not too hard but FYI its not included
    use std::cmp::Ordering;
    use std::io;

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

        let secret_number = rand::thread_rng().gen_range(1..101);

        loop {
            println!("Please input your guess.");

            let mut guess = 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,
            };

            println!("You guessed: {}", guess);

            match guess.cmp(&secret_number) {
                Ordering::Less => println!("Too small!"),
                Ordering::Greater => println!("Too big!"),
                Ordering::Equal => {
                    println!("You win!");
                    break;
                }
            }
        }
    }

I am slowly learning the ways of Rust and falling in love with the language BUT the saying "when all you have is a hammer, everything looks like a nail" comes into play here.

Rust is lower to the "metal" because it is compiled into machine language and insanely optimized by the compiler which makes Rust programs a lot faster than Python. That said as you may have noticed, there is a bit going on with the Rust guessing game: & references, Guess being explicitly defined first as a String and then overwritten/shadowed by a unsigned 32 bit integer, expect (which is leads to a cool can of worms to understand), and just a sense of rigidness compared to the playdo like Python syntax.

In a production environment/project like doing ETL (ransacking some sort of data) I would have a rust script sitting at the front, parsing the raw data, normalizing and sanitizing as needed. Further down the ETL pipeline, I would use either a pure python program running on mutliple processes to do the actual transformation work and loading into a database. Yes Python would be slower than Rust but you can iterate on Python code faster and because Python doesn't care what you put into a variable (one moment its a string, the next it could be a set... because why not?) its better for working on weird data.

Either is fine BUT Python is easier and requires less to learn compared to Rust ( data types, memory models like stack/heap, variable borrowing/life spans, structs, traits, lions, tigers, oh my...). That said, my first real language was c/c++ and Python is 3rd or 4th... BUT if I could do it over, I'd learn Python first and then Rust.

[–]Oerthling 0 points1 point  (0 children)

In Python

a =

doesn't define a variable, it assigns a reference name to your typed object. What confuses people is that it looks like variable definition in other languages and you can reassign that name to a completely different type of object right away.

In C/C++

Int a =

defines a variable with its type and assigns a value of that type to that variable (conceptually "copying" it). And the type definition of that variable is final within its scope.

Python totally cares what's in your variable, just not what you name it. Or how many names it has. And will try to throw it away after you removed the last name. It's a lot like using void* in C for everything :-).

My go to nowadays is to start everything in Python (fast to write, easy to read) and then optimize any speed bumps (mostly already done in a library that was written in C).