"Hello World" of Rust

  ewe......たつた三十行足らずのsample code[introduction]に転がされる事三十分と少々. これで色々と実際に書いて試せる所へ. 後出し法に乗せられてstack overflowを回つてしまう辺り, 気が早過ぎるかも.
  結論は, きちんと説明文を読み進めてから取りかかれ, と謂う事で.

main.rs

extern crate rand;

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

fn main(){
  guess();
}

fn guess(){
  println!("please input your number\n");
  let mut g = String::new();
  io::stdin()
    .read_line(&mut g)
    .ok()
    .expect("failed");
  let secret_number: i32 = rand::thread_rng()
    .gen_range(1, 101);
  let n:i32 = g.trim()
    .parse()
    .expect("wanted a num");
  match n.cmp(&secret_number) {
    Ordering::Less    => println!("bigger than your number"),
    Ordering::Equal   => println!("equal"),
    Ordering::Greater => println!("smaller than your number")
  };
  println!("correct number is {}", secret_number);
}



Cargo.toml

[package]
name = "hello"
version = "0.1.0"
authors = [“author_name”]

[dependencies]
rand="=0.3.15"

f:id:blau_coerulea:20170415123522p:plain

Remove all ads