Linux Magazine #1 2022: Rust Listings
- February 16, 2022
- 0
C-programma Listing:
#include <stdio.h> #include <stdlib.h> #include <ctype.h> #include <time.h> #include <unistd.h> struct ticketnum { int lastnum; int forlastnum; }; int main () { int t,l; srand((unsigned)time(NULL)); printf("\n\tShow me the Money!\n"); printf("\tA tool to generate the last two numbers for your lottery ticket.\n\n"); printf("\tHow many ticket numbers do you want to generate? "); scanf("%d", &t); if (t == 0) { printf("\n\tYou did not enter a valid amount of tickets.\n"); printf("\tClosing the program.\n"); return 1; } printf("\n\tYou have choosen to generate random number for %d lottery tickets.\n", t); printf("\tHere we go!!\n\n"); sleep(1); struct ticketnum ticket[t]; l = t; do { ticket[l].forlastnum = rand() % 10; ticket[l].lastnum = rand() % 10; printf("\tTicket number %d has the following two number at the end:\n", l); printf("\t %d %d\n", ticket[l].forlastnum, ticket[l].lastnum); l=l-1; sleep(1); } while (l>0); printf("\n\tGood Luck!\n"); return(0); }
Rust-programma Listing:
use std::io; use rand::Rng; use std::{thread, time}; struct Ticketnum { lastnum: u8, forlastnum: u8, } fn main() { let mut x = 0; let mut input = String::new(); println!("Show me the Money!"); println!("Een klein programma om de laatste twee nummers van je staatslot te genereren."); println!("Hoeveel staatsloten wil je genereren?"); io::stdin().read_line(&mut input).expect("Failed to read line."); let tickets: u8 = input.trim().parse().unwrap_or(0); println!("Je hebt ervoor gekozen om voor {} tickets nummers te genereren", tickets); while x < tickets { thread::sleep(time::Duration::from_secs(2)); println!("Bezig met genereren van ticket {}", x+1); x+=1; let ticketgen = Ticketnum { lastnum: rand::thread_rng().gen_range(0..9), forlastnum: rand::thread_rng().gen_range(0..9), }; println!("Ticket nummer {} heeft de volgende twee nummer op het eind: {} {}", x, ticketgen.forlastnum, ticketgen.lastnum); } }