聲明: 本文是在參考 The Rust Programming Language 和 Rust官方教程 中文版 寫的。 我的學習用html
再PS. 目錄這東東果真是必須的... 找個時間生成個python
cargo new projectName --bin
建立一個工程cargo build
和 cargo run
命令變量綁定有它們所綁定的的值的全部權。這意味着當一個綁定離開做用域,它們綁定的資源就會被釋放。c++
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是一門靜態隱式類型的語言。git
類型在編譯時推導, 相似也c++11的
auto
特性數組
Rust確保了對於任何給定的資源都只有一個綁定與之對應。python2.7
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); // 不報錯
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` }
如何解決這個問題?學習
fn main() { fn fn1(arg: &Vec<i32>) -> u32 { // 需傳入一個引用 21 } let a = vec![21, 32]; fn1(&a); // 傳入&T類型,一個引用類型 println!("{}", a[0]); }
上述的借用都是immutable
借用類型, 還有&mut
類型。
Rust的借用有一些必須遵照的規則:
在同一做用域中ui
&T
&mut
緣由: 在編譯時避免數據競爭...this
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);
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
前面見過的有一個引用類型做爲參數的函數,之因此沒有看到聲明週期這東東。 是由於聲明週期省略形成的錯覺。
咱們能夠以 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); }
應當注意的地方:
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
let x: &'static str = "Hello, world.";
static FOO: i32 = 10; // 定義一個常量 let x: &'static i32 = &FOO; println!("{}", *x);
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, } }
跟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中很是強大,以上只介紹了它的幾種用法。
類型 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中是不容許的
---恢復內容結束---