Skip to content

Instantly share code, notes, and snippets.

@rust-play
Created August 28, 2018 04:52
Show Gist options
  • Save rust-play/81fc2bcfe7e5a3681ee33de11ce7ac1e to your computer and use it in GitHub Desktop.
Save rust-play/81fc2bcfe7e5a3681ee33de11ce7ac1e to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#![allow(dead_code)]
#![allow(unused_variables)]
#[macro_use]
extern crate log;
#[derive(Debug)]
enum Person {
// 一个 `enum` 可能是个 `unit-like`(类单元结构体),
Engineer,
Scientist,
// 或像一个元组结构体,
Height(i32),
Weight(i32),
// 或像一个普通的结构体。
Info { name: String, height: i32 }
}
enum R {
RIGHT = 1,
WRONG,
}
trait MyTrait {
fn print(&self, message: &str);
fn print2(&self, message: &str, r: R);
}
struct Data {
a: String
}
impl MyTrait for Data {
fn print(&self, message: &str) {
println!("{}: {}", self.a, message);
}
fn print2(&self, message: &str, r: R) {
println!("{}: {} {}", self.a, message, r as u8);
}
}
fn main() {
let person = Person::Info {
name: "mmzhou".to_string(),
height: 180
};
match person {
Person::Info {name, height} => {
println!("{}'s height is {}", name, height);
},
_ => println!("Not found")
}
debug!("Person::Engineer = {:?}", Person::Engineer);
let d1 = Data {a: "d1 test".to_string()};
d1.print("test");
let d2: &MyTrait = &Data {a: "d2 test".to_string()};
d2.print2("test", R::RIGHT);
let i = 1;
let j = &i;
let m = *j;
println!("Number: {}", m);
println!("Number: {}", i);
let s = "hello";
println!("length: {}", s.len());
println!("length: {}", (&s).len());
println!("length: {}", (&&&&&&&&&&&&&s).len());
match 1 {
_ => println!("match _ test {:?}", _),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment