[spam] language learning: rust

Undiscussed Horrific Abuse, One Victim of Many gmkarl at gmail.com
Sun Apr 24 02:42:11 PDT 2022


>
> Homework:
> 1. Implement and run the guessing example.
>

$ cat 0101-guessing-game/src/main.rs
use rand::Rng;
use std::cmp::Ordering;

fn main() {

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

    let mut guess = String::new();

    loop {
        println!("Guess a number: ");

        guess.clear();
        std::io::stdin().read_line(&mut guess).expect("error reading line");

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

        match secret_number.cmp(&guess) {
            Ordering::Less => println!("Try lower."),
            Ordering::Greater => println!("Try higher."),
            Ordering::Equal => {
                println!("That's it: {}", secret_number);
                break;
            }
        }
    }

}


2. Make a rust project that uses std::io to generate a .jl julia script
> that performs and outputs the result of an arithmetic calculation.
>
> file output: https://doc.rust-lang.org/stable/std/io/
>

I ended up using https://docs.rust-lang.org/stable/std/fs/

julia: see other thread with similar name
>

$ cat 0102-output-julia/src/main.rs
use std::io::Write;

fn main() {
    let mut f : std::fs::File =
std::fs::File::create("output.jl").expect("failed to create output.jl");
    f.write_all(b"x = 4\nprintln(\"4 + 4 = $(x + 4)\")\n").expect("failed
to write lines");
}

$ julia output.jl
4 + 4 = 8

>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/html
Size: 4562 bytes
Desc: not available
URL: <https://lists.cpunks.org/pipermail/cypherpunks/attachments/20220424/c0ced48f/attachment.txt>


More information about the cypherpunks mailing list