Rust語言學習筆記(2)
阿新 • • 發佈:2019-02-01
結構體(structs)
結構體有三種形式:- 沒有成員的單元結構體
- 有成員但沒有名字的元組結構體
- 有成員而且有名字的普通結構體
// 結構體 struct Point { x: i32, y: i32, } // 不變繫結 let origin = Point { x: 0, y: 0 }; // origin: Point println!("The origin is at ({}, {})", origin.x, origin.y); // 可變繫結 let mut point = Point { x: 0, y: 0 }; point.x = 5; // 更新語法(update syntax) struct Point3d { x: i32, y: i32, z: i32, } let mut point = Point3d { x: 0, y: 0, z: 0 }; point = Point3d { y: 1, .. point }; // 元組結構體(tuple structs) struct Color(i32, i32, i32); struct Point(i32, i32, i32); let black = Color(0, 0, 0); let origin = Point(0, 0, 0); // 單元結構體(unit-like structs) struct Electron; let x = Electron;
方法語法(method-syntax)
// 方法語法 struct Circle { x: f64, y: f64, radius: f64, } impl Circle { // 要實現方法呼叫(method calls)語法,第一個引數必須是self的某種形式 fn area(&self) -> f64 { std::f64::consts::PI * (self.radius * self.radius) } // 通過返回結構體來實現鏈式方法呼叫(chaining method calls) fn grow(&self, increment: f64) -> Circle { Circle { x: self.x, y: self.y, radius: self.radius + increment } } // 通過返回結構體來實現關聯函式(associated functions) fn new(x: f64, y: f64, radius: f64) -> Circle { Circle { x: x, y: y, radius: radius, } } } impl Circle { // self引數只有不變借用,可變借用和移動這三種形式 fn reference(&self) { println!("taking self by reference!"); } fn mutable_reference(&mut self) { println!("taking self by mutable reference!"); } fn takes_ownership(self) { println!("taking ownership of self!"); } } fn main() { let c = Circle { x: 0.0, y: 0.0, radius: 2.0 }; // 方法呼叫 println!("{}", c.area()); // 關聯函式 let c = Circle::new(0.0, 0.0, 2.0); // 鏈式方法呼叫 let d = c.grow(2.0).area(); println!("{}", d); }
列舉(enums)
列舉其實是一種變體結構在列舉中可以定義所有三種結構體
// 列舉
enum Message {
Quit,
ChangeColor(i32, i32, i32),
Move { x: i32, y: i32 },
Write(String),
}
let x: Message = Message::Move { x: 3, y: 4 };
let m = Message::Write("Hello, world".to_string());
let v = Message::ChangeColor(r, g, b);
模式(patterns)匹配(match)
// 匹配常量
let x = 1;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
_ => println!("anything"),
}
// 匹配多個模式
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
// 匹配結構
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: 0, y: 0 };
match origin {
Point { x, y } => println!("({},{})", x, y),
}
match origin {
Point { x: x1, y: y1 } => println!("({},{})", x1, y1),
}
match origin {
Point { x, .. } => println!("x is {}", x),
}
match origin {
Point { y, .. } => println!("y is {}", y),
}
// 匹配列舉
enum Message {
Quit,
ChangeColor(i32, i32, i32),
Move { x: i32, y: i32 },
Write(String),
}
fn quit() { /* ... */ }
fn change_color(r: i32, g: i32, b: i32) { /* ... */ }
fn move_cursor(x: i32, y: i32) { /* ... */ }
fn process_message(msg: Message) {
match msg {
Message::Quit => quit(),
Message::ChangeColor(r, g, b) => change_color(r, g, b),
Message::Move { x: x, y: y } => move_cursor(x, y),
Message::Write(s) => println!("{}", s),
};
}
// 匹配中忽略某些值
// 匹配Result<R,E>時忽略某些值
match some_value {
Ok(value) => println!("got a value: {}", value),
Err(_) => println!("an error occurred"),
}
// 匹配元組時忽略某些值
fn coordinate() -> (i32, i32, i32) {
// generate and return some sort of triple tuple
}
let (x, _, z) = coordinate();
let tuple: (u32, String) = (5, String::from("five"));
let (x, s) = tuple; // 匹配時String被移動
let (x, _) = tuple; // 匹配時String被忽略
// 匹配列舉時忽略某些值
enum OptionalTuple {
Value(i32, i32, i32),
Missing,
}
let x = OptionalTuple::Value(5, -2, 3);
match x {
OptionalTuple::Value(..) => println!("Got a tuple!"),
OptionalTuple::Missing => println!("No such luck."),
}
// 匹配後得到不變引用
let x = 5;
match x {
ref r => println!("Got a reference to {}", r),
}
// 匹配後得到可變引用
let mut x = 5;
match x {
ref mut mr => println!("Got a mutable reference to {}", mr),
}
// 匹配區間
let x = 1;
match x {
1 ... 5 => println!("one through five"),
_ => println!("anything"),
}
let x = ',';
match x {
'a' ... 'j' => println!("early letter"),
'k' ... 'z' => println!("late letter"),
_ => println!("something else"),
}
// 匹配時繫結區間到變數名
let x = 1;
match x {
e @ 1 ... 5 | e @ 8 ... 10 => println!("got a range element {}", e),
_ => println!("anything"),
}
// 匹配複雜結構體時繫結某一部分到變數名
#[derive(Debug)]
struct Person {
name: Option,
}
let name = "Steve".to_string();
let mut x: Option = Some(Person { name: Some(name) });
match x {
Some(Person { name: ref a @ Some(_), .. }) => println!("{:?}", a),
_ => {}
}
// 匹配守衛(guards)即匹配時新增條件限制
enum OptionalInt {
Value(i32),
Missing,
}
let x = OptionalInt::Value(5);
match x {
OptionalInt::Value(i) if i > 5 => println!("Got an int bigger than five!"),
OptionalInt::Value(..) => println!("Got an int!"),
OptionalInt::Missing => println!("No such luck."),
}
// 守衛的優先順序比多重模式要低
let x = 4;
let y = false;
match x {
4 | 5 if y => println!("yes"),
_ => println!("no"),
}