use std::{fs::File, io::Read};
use gw_rust_programming_tutorial::{eat_at_restaurant, test_chapter_10, test_chapter_12, test_chapter_13, test_chapter_8, test_chapter_9};
fn main() {
let args: Vec<String> = std::env::args().collect();
println!("args={:?}",args);
let mut file = File::open("c:\\1.txt").expect("fail to open file");
let mut buffer = String::new();
file.read_to_string(&mut buffer).expect("fail to read file");
let lines: Vec<&str> = buffer.lines().collect();
for li in lines {
println!("{}", li);
}
for i in 2..5{
println!("for 2..5 i={}",i);
}
let x = 5;
println!("{}", x);
let x = "asd";
println!("{}", x);
let num: i32 = "12346".parse().expect("not is number");
println!("{}", num);
let fnum: f32 = 3.0;
let ch: char = 'a';
let tu: (i32, f64, u8) = (32, 65.5, 12);
println!("{:#?}", tu);
println!("{:?}", tu);
let tu1 = (500, 6.5, 1);
let (x, y, z) = tu1;
println!(
"{:?} x={} y = {} z={} index=0 value={}",
tu1, x, y, x, tu1.0
);
test_arr();
test_lanmbda();
let x = test_ret(15);
println!("{}", x);
let x = test_ret1(16);
println!("{}", x);
test_control(15);
test_loop();
test_loop_ret_val();
test_while();
test_for();
test_ownerships();
test_move_ownership();
test_ref();
test_slice();
test_struct();
test_uoname_field_struct();
test_struct_method();
test_enum();
test_enum_match();
test_optionT_match();
eat_at_restaurant();
test_chapter_8();
test_chapter_9();
test_chapter_10();
test_chapter_12();
test_chapter_13();
println!("Hello, world end running!");
}
fn test_arr() {
let arr = [1, 2, 3, 4, 5];
println!("arr={:?}", arr);
let arr: [&str; 2] = ["aaa", "bbb"];
println!("arr={:?}", arr);
let arr = [0i8; 5];
println!("arr={:?} 0={} 1={}", arr, arr[0], arr[1]);
}
fn test_lanmbda() {
let y = {
let x = 1;
x + 1
};
println!("{}", y);
}
fn test_ret(x: i32) -> i32 {
x + 1
}
fn test_ret1(x: i32) -> i32 {
return x + 1;
}
fn test_control(x: i32) {
if x % 2 == 1 {
println!("偶数");
} else {
println!("奇数");
}
}
fn test_loop() {
let mut n = 10;
'out: loop {
println!("out loop");
loop {
println!("inner loop");
if {
n -= 1;
n
} == 0
{
break 'out;
}
}
}
}
fn test_loop_ret_val() {
let mut count = 0;
let ret = loop {
if {
count += 1;
count
} == 10
{
break count * 2;
}
};
println!("loop ret={}", ret);
}
fn test_while() {
let mut n = 5;
while {
n -= 1;
n
} != 0
{
println!("n={}", n);
}
}
fn test_for() {
for e in (1..4).rev() {
println!("e={}", e);
}
}
fn test_ownerships() {
let s = String::from("hello");
takes_ownership(s);
let x = 5;
makes_copy(x); println!("{}", x);
}
fn takes_ownership(some_string: String) {
println!("{}", some_string);
}
fn makes_copy(some_integer: i32) {
println!("{}", some_integer);
}
fn test_move_ownership() {
let s1 = gives_ownership();
let s2 = String::from("hello");
let s3 = takes_and_gives_back(s2); }
fn gives_ownership() -> String {
let some_string = String::from("yours");
some_string }
fn takes_and_gives_back(a_string: String) -> String {
a_string }
fn test_ref() {
let mut s = String::from("hello");
change(&mut s);
println!("{}", s);
}
fn change(some_string: &mut String) {
some_string.push_str(", world");
}
fn first_word(s: &String) -> &str {
let bytes = s.as_bytes();
for (i, &item) in bytes.iter().enumerate() {
if item == b' ' {
return &s[0..i];
}
}
&s[..]
}
fn test_slice() {
let s = String::from("hello world");
let s = first_word(&s);
println!("{}", s);
}
#[derive(Debug)]
struct User {
active: bool,
username: String,
email: String,
sign_in_count: u64,
}
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
fn test_struct() {
let mut user1 = User {
email: String::from("someone@example.com"),
username: String::from("someusername123"),
active: true,
sign_in_count: 1,
};
println!("user={:#?}", user1);
user1.active = false;
println!("user={:#?}", user1);
let user1 = build_user(
String::from("someone@example.com"),
String::from("someusername123"),
);
println!("{:?}", user1);
let user2 = User {
email: String::from("eee@163.com"),
..user1
};
println!("user2={:?}", user2);
}
#[derive(Debug)]
struct Color(i32, i32, i32);
#[derive(Debug)]
struct Point(i32, i32, i32);
fn test_uoname_field_struct() {
let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
println!("color={:?} point={:?}", black, origin);
}
#[derive(Debug)]
struct Rectangle {
width: u32,
height: u32,
}
impl Rectangle {
fn area(&self) -> u32 {
self.width * self.height
}
}
fn test_struct_method() {
let rect1 = Rectangle {
width: 30,
height: 50,
};
println!(
"The area of the rectangle({:?}) is {} square pixels.",
rect1,
rect1.area()
);
}
#[derive(Debug)]
enum Message {
Quit,
Move { x: i32, y: i32 },
Write(String),
ChangeColor(i32, i32, i32),
Ooo(Option<i8>),
}
impl Message {
fn call(&self) {
println!("call message");
}
}
fn test_enum() {
let msg = Message::Ooo(Some(15));
msg.call();
println!("{:?}", msg);
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => {
println!("Lucky");
1
}
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn test_enum_match() {
let c = Coin::Penny;
let c = value_in_cents(c);
println!("{}", c);
}
fn test_optionT_match() {
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
let five = Some(5);
let six = plus_one(five);
let none = plus_one(None);
println!("{:?} {:?} {:?}",five,six,none);
}