Rust入門篇
聲明: 本文是在參考 The Rust Programming Language 和 Rust官方教程 中文版 寫的。 個人學習用
再PS. 目錄這東東果然是必須的... 找個時間生成個
Hello World
- 使用
cargo new projectName --bin
創建一個工程 cargo build
和cargo run
命令- cargo配置文件: 工程下的 Cargo.toml 文件
所有權
變量綁定
變量綁定有它們所綁定的的值的所有權。這意味着當一個綁定離開作用域,它們綁定的資源就會被釋放。
let a = vec![21]; // let聲明一個變量綁定,非變量
a.push(90); // error: cannot borrow immutable local variable `a` as mutable 對象默認是immutable
let a = 'x'; // a 重新綁定一個對象
a = 'a'; // error: re-assignment of immutable variable `a`
- 拓展:Rust是一門靜態隱式類型的語言。
類型在編譯時推導, 類似也c++11的
auto
特性
移動語義
Rust確保了對於任何給定的資源都只有一個綁定與之對應。
let a = vec![1, 2];
let b = a; // 將a綁定的對象所有權交給b.
println!("{}", a[0]); // error: use of moved value: `a`
拷貝語義
同其他C-style語言一樣, Rust的基本類型具有copy語義
let a = 32;
let b = a;
println!("{}", a); // 不報錯
借用(Borrowing)
- 引子:
fn main() {
fn fn1(arg: Vec<i32>) -> u32 { // 函數的定義格式...
21 // 表達式可以返回一個值
}
let a = vec![21, 32];
fn1(a); // 將a綁定的對象所有權傳入函數中...
println!("{}", a[0]); // use of moved value: `a`
}
如何解決這個問題?
1. 使用 borrowing
fn main() {
fn fn1(arg: &Vec<i32>) -> u32 { // 需傳入一個引用
21
}
let a = vec![21, 32];
fn1(&a); // 傳入&T類型,一個引用類型
println!("{}", a[0]);
}
上述的借用都是immutable
借用類型, 還有&mut
類型。
Rust的借用有一些必須遵守的規則:
在同一作用域中
- 一個或者多個對資源的引用
&T
- 只有一個mutable引用
&mut
原因: 在編譯時避免數據競爭...
- 例子:
let mut x = 5;
let y = &mut x;
*y += 1;
println!("{}", x); // cannot borrow `x` as immutable because it is also borrowed as mutable
不過,解決這個問題的方法是... 縮小y的作用范圍:
let mut x = 5;
{
let y = &mut x;
*y += 1;
}
println!("{}", x);
2. 對象克隆
fn main() {
fn fn1(arg: Vec<i32>) -> u32 {
21
}
let a = vec![21, 32];
fn1(a.clone()); // 將a的副本傳入即可
println!("{}", a[0]); // use of moved value: `a`
}
生命周期
在Rust中,引用必須與它引用的資源存活得一樣長!
如下兩例子:
let r : &i32;
{
let a = 32;
r = &32; // error: borrowed value does not live long enough
}
println!("{}", r);
let r : &i32;
let x = 78;
r = &x; // error: `x` does not live long enough
- 注意在Rust中 生命周期 這概念是與引用/借用緊密關聯的
- 它定義了引用有效的作用域。
前面見過的有一個引用類型作為參數的函數,之所以沒有看到聲明周期這東東。 是因為聲明周期省略造成的錯覺。
我們可以以 implicit
或者 explicit
的方式來定義一個函數:
// implicit
fn foo(x: &i32) -> &i32{
}
// explicit
fn bar<'a>(x: &'a i32) -> &'a i32{
}
此外,結構體(struct)也擁有生命周期。
接下來解決struct
后再繼續...
類型
結構體
一個簡單的struct:
struct Point {
x: i32, // Note: 逗號作為分隔符
y: i32,
}
fn main() {
let origin = Point { x: 0, y: 0 };
println!("The origin is at ({}, {})", origin.x, origin.y);
}
應當注意的地方:
- struct不支持字段可變性。因此不能在字段上添加
mut
修飾 - 可變性是綁定的一個屬性, 讓變量在一段時間內可變
為啥這樣設計, 舉個例子:
struct Point {
x: i32,
y: i32,
}
fn main() {
let mut point = Point { x: 0, y: 0 };
point.x = 5;
let point = point; // this new binding can’t change now
point.y = 6; // this causes an error
}
生命周期 · 續
當結構體中具有引用類型的屬性時, 結構體就需要使用顯示的生命周期。
錯誤示例:
struct Foo {
x: &i32, // error: missing lifetime specifier
}
正確的寫法:
struct Foo<'a> {
x: &'a i32,
}
fn main() {
let y = &5; // 等價於 `let _y = 5; let y = &_y;`
let f = Foo { x: y };
println!("{}", f.x);
}
為什么Foo
需要一個生命周期? 因為我們需要確保Foo
中的任何引用不能比它包含的 i32 的引用活的更久。
impl
塊
使用impl
在Foo中定義一個方法:
fn main() {
let y = &5;
let f = Foo { x: y };
println!("{}", f.x());
}
struct Foo<'a> {
x: &'a i32,
}
impl<'a> Foo<'a> { // 標點符號嚇死人系列...
fn x(&self) -> &'a i32 { self.x }
}
'a
就是用來賦予作用域一個名字。
下面介紹一個特殊的命名作用域:
-
'static
- 在Rust中最常見的:
let x: &'static str = "Hello, world.";
static FOO: i32 = 10; // 定義一個常量 let x: &'static i32 = &FOO; println!("{}", *x);
- 在Rust中最常見的:
方法語法
struct Circle {
x: f64,
y: f64,
radius: f64,
}
impl Circle {
fn area(&self) -> f64 {
std::f64::consts::PI * (self.radius * self.radius)
}
}
fn main() {
let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
println!("{}", c.area());
}
方法的第一個參數比較特殊。它有3種變體: `self`, `&self` 和 `&mut self`。 通常使用后兩種! 當方法只是讀取struct中的數據時使用`&self`。 若要修改數據則使用`&mut self`。
-
關聯函數
不帶self參數的方法就是關聯函數。 這是一個Rust代碼中非常常見的模式。
impl Circle { fn new(x: f64, y: f64, radius: f64) -> Circle { Circle { x: x, y: y, radius: radius, } }
- 關聯函數的調用: `let c = Circle::new(0.0, 0.0, 2.0);
- 關聯函數的調用: `let c = Circle::new(0.0, 0.0, 2.0);
-
創建者模式 Builder Pattern
枚舉
跟C
不同,Rust的枚舉可攜帶數據.... 看個例子
enum Message {
Quit,
ChangeColor(i32, i32, i32),
Move {x: i32, y: i32},
Write(String),
}
// 使用 match 來實現類型的轉換
fn process_message(msg: Message) -> i32{
match msg { // match所有分支返回類型必須一致
Message::Quit => 32, // 逗號隔開
Message::ChangeColor(r,g,b) => r+g+b,
Message::Move{x: x1, y: y1} => x1 + y1,
Message::Write(s) => s.trim().parse().ok().expect("parse error!"),
}
}
fn main() {
let a = Message::Quit;
let b = Message::ChangeColor(1, 2, 3);
let c = Message::Move{x: 32, y: -32};
let d = Message::Write("88".to_string());
println!("{}", process_message(a));
println!("{}", process_message(b));
println!("{}", process_message(c));
println!("{}", process_message(d));
}
匹配和模式
let x = 5;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
4 => println!("four"),
5 => println!("five"),
_ => println!("something else"),
}
Rust編譯器檢查窮盡性,要求對每一個枚舉的變量都有一個匹配分支。如果你忽略了一個,除非你用_
否則它會給你一個編譯時錯誤。
模式
在匹配語句中使用到:
let my_number = 8;
match my_number {
0 => println!("zero"),
1 | 2 => println!("one or two"), // Multiple patterns
3 ... 10 => println!("three to ten"), // Ranges
_ => println!("something else")
}
解構: 對於復合數據類型, 可以在模式中進行解析
struct Point {
x: i32,
y: i32,
}
let origin = Point { x: -9, y: 0=77 };
match origin {
Point { x, y } => println!("({},{})", x, y),
}
// 解析部分值 使用 .. 來忽略部分或所有值
match origin {
Point { x, .. } => println!("x is {}", x),
}
忽略綁定
fn fn1() -> (i32, i32) {
(33, 43)
}
let (i, _ ) = fn1(); // 只綁定fn1第一個值, 忽略第二個值的綁定
println!("{}", i);
模式在Rust中非常強大,以上只介紹了它的幾種用法。
Vector
類型 Vec<T>, vector總是在堆上分配數據! 可以使用vec!宏來創建。
let v = vec![1, 2, 3, 4, 5]; // v: Vec<i32>
let v = vec![0; 10]; // ten zeroes
越界訪問
let v = vec![32, 43];
println!("{:?}", v[3]); // 運行時 thread '<main>' panicked at 'index out of bounds
迭代
let mut v = vec![1, 2, 3, 4, 5];
for i in &v {
println!("A reference to {}", i);
}
方法
let v = vec![43, 54, 65]; // v: Vec<i32>
// 數組長度
println!("{:?}", v.len());
字符串
Rust有兩種主要的字符串類型:&str
和String
。
同 C-style 系, let greeting = "Hello there."; // greeting: &'static str
&str編譯后存儲在程序中, 在運行期間一直存在。
String
則不同,是一個在堆上分配的字符串。這個字符串可以增長,並且也保證是UTF-8編碼的。
let mut s = "Hello".to_string(); // mut s: String
println!("{}", s);
s.push_str(", world.");
println!("{}", s);
String可以通過一個&強制轉換為&str
:
let tmp = "鬼".to_string();
let s = "什么".to_string() + &tmp; // String + str => String
println!("{:?}", s);
題外話: 被惡心到了... str + str 和 String + String 是不被允許的
不懂為啥這樣設計
Note : 由於let s = "hello";
中"hello"是一個UTF-8
編碼的字符串,故不能直接用索引來訪問字符串的元素。 編碼掃盲篇
關於Rust的字符串(如"hello"), 就好像你在ipython中輸入:
注意這里使用的是 python2.7
> a = '嚴'
> a
> '\xe4\xb8\xa5'
> len(a)
> 3
在python中你可以使用a[2]
來訪問a指向的str。 但這在Rust中是不允許的
---恢復內容結束---