Rust-lang(hello world)

最近,突然迷上了Rust!Rust在中国还是小众中的小众!有个一年不会更新一次的论坛,没有任何中文,资料,只能去死磕官方文档!这个就是我死磕官方文档的笔记,是笔记,笔记,不是翻译!

install

略过吧,官网一看就会安装!

hello world

fn main(){
    println!("{}","hello world");
}

使用命令rustc main.rs 编译, ./main 运行。

cargo

Rust 自带的项目构建工具。

$ mkdir src
$ mv main.rs src/main.rs
$ rm main 

在工程目录下创建文件Cargo.toml,然后配置它。

[package]
name = "rust-study"
version = "0.1.0"
authors = ["madong"]

[dependencies]
rand="0.3.0"

main.rs code:

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

fn main() {
    println!("Hello, world!");

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

    println!("Please gusses secret number  !");

    let mut number = String::new();

    io::stdin().read_line(&mut number).expect("Failed to read_line");

    let number : u32 = number.trim().parse().expect("Please type a number");

    println!("Hello,your number is {}", number);

    println!("The secret number is: {}", secret_number);

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

}

在工程目录下运行,cargo run 即可运行程序!

结束 hello world!

转载于:https://www.cnblogs.com/re-myself/p/5532477.html

你可能感兴趣的:(rust)