rust官方的猜字游戲
struct User {
username :String,
email:String,
sign_in_count: u64,
active:bool,
}
fn build_user(email: String, username: String) -> User {
User {
email,
username,
active: true,
sign_in_count: 1,
}
}
#[derive(Debug)]
struct PPP {
w: u32,
h: u32,
}
impl PPP {
fn new(w :u32, h :u32) -> PPP {
PPP{w:w, h:h}
}
fn area(&self) -> u32 {
self.w * self.h
}
fn campare(&self, other: &PPP) -> bool {
self.area() > other.area()
}
}
enum IpAddrKind {
V4,
V6
}
struct IpAddr {
kind:IpAddrKind,
address:String,
}
enum IpAddrV2 {
V4(u8,u8,u8,u8),
V6(String),
}
enum Coin {
Penny,
Nickel,
Dime,
Quarter,
}
fn value_in_cents(coin: Coin) -> u8 {
match coin {
Coin::Penny => 1,
Coin::Nickel => 5,
Coin::Dime => 10,
Coin::Quarter => 25,
}
}
fn main() {
let x = 123;
println!("{x}");
let x = 456;
println!("{}", x);
let mut x = 758;
x = 111;
println!("{}", x);
const MAX_POINTS: u32 = 100_000;
println!("MAX_POINTS:{}", MAX_POINTS);
let y :i64 = 12;
let mut x :i32 = y as i32;
x = 0xff;
println!("x:{}", x);
let x :f32 = 12.12;
println!("{}", x);
let b:bool = true;
println!("b={}", b);
let tup1 :(i32,i64,u8) = (1,2,3);
println!("tup1={:?}", tup1);
let arr = [1,2,4,5];
println!("arr={:?}", arr);
let arr2 :[f32;5]= [1.1,2.2,2.3,4.1,5.2];
println!("arr={:?}", arr2);
//func
let x :i32= 123;
afunc(x);
println!("func1 {}", addOne(x));
if x > 3 {
println!("big then 3");
}else if (x > 4) {
println!("small then 4");
}else {
println!("small then 3");
}
let number = if x > 3 {
true
} else {
false
};
println!("number={}", number);
let mut x = 1;
loop {
if x > 3 {
break;
}
x = x + 1;
println!("again!");
}
while x < 6 {
x = x + 1;
println!("again!");
}
for elem in arr.iter() {
println!("elem={}", elem);
}
for elem in (1..4).rev() {
println!("rev={}", elem);
}
{
let xxx = 1;
println!("inner xxx={}", xxx);
}
//println!("xxx={}", xxx);
let mut str1 = String::from("hello");
str1.push_str(" world!");
println!("{}", str1);
let str2 = str1;
println!("str2:{}", str2);
//clone
let str3 = str2.clone();
println!("str2:{}", str2);
println!("str3:{}", str3);
let str11 = String::from("hello");
let str12 = setStr(str11);
//println!("str11:{}", str11);
println!("str12:{}", str12);
/**
* 在任意給定時間背捌,要么 只能有一個可變引用狐胎,要么 只能有多個不可變引用溪椎。
引用必須總是有效的。
*/
//add str
let mut str13 = String::from("abc");
addStr2(&mut str13);
println!("str13={}", str13);
let str14 = &str13[0..5];
let str15 = &str13[5..8];
println!("str14={} {}", str14,str15);
println!("all={}", &str13[..]);
let user1 = User{
username :String::from("xxx"),
email:String::from("xxx@126.com"),
sign_in_count: 41,
active:true,
};
let user2 = build_user(String::from("xxx"),String::from("xxx@126.com"));
println!("user1.name={}", user1.username);
println!("user2.name={}", user2.username);
struct Color(i32, i32, i32);
struct Point(i32, i32, i32);
let black = Color(1, 2, 3);
let origin = Point(11, 12, 13);
println!("point1={:?} {:?}", black.0,origin.0);
let ppp1 = PPP::new(11,12);
println!("ppp1={:?}, {:?}", ppp1, ppp1.area());
let ppp2 = PPP::new(12,12);
println!("ppp2={:?}, {:?}", ppp2, ppp2.area());
println!("bool={}",ppp1.campare(&ppp2));
let home = IpAddr {
kind: IpAddrKind::V4,
address: String::from("127.0.0.1"),
};
println!("home:{}",home.address);
let loopback = IpAddr {
kind: IpAddrKind::V6,
address: String::from("::1"),
};
println!("home:{}",loopback.address);
let ipv2 = IpAddrV2::V4(1,2,3,4);
let some_number = Some(5);
let some_string = Some("a string");
let absent_number: Option<i32> = None;
let coin_one = Coin::Penny;
let mut count = 0;
if let Coin::Penny = coin_one {
println!("Coin::Penny match");
} else {
count += 1;
}
println!("count:{}",count);
let result = value_in_cents(coin_one);
println!("result:{}",result);
let some1 = Some(6);
let some2 = plus_one(some1);
let none = plus_one(None);
let some_u8_value = 0u8;
match some_u8_value {
1 => println!("one"),
3 => println!("three"),
5 => println!("five"),
7 => println!("seven"),
_ => (),
}
}
fn plus_one(x :Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
}
fn setStr(str1 : String) -> String{
str1
}
fn printLen(str :&String) -> usize {
str.len()
}
fn addStr2(str :&mut String){
str.push_str(", world");
}
fn afunc(x :i32) {
println!("func1 {}", x);
}
fn addOne(x :i32) -> i32{
x + 1
}