声明: 本文是在参考 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中是不容许的
---恢复内容结束---