Rust入门篇 (1)

Rust入门篇


声明: 本文是在参考 The Rust Programming LanguageRust官方教程 中文版 写的。 个人学习用

再PS. 目录这东东果然是必须的... 找个时间生成个

Hello World

  1. 使用 cargo new projectName --bin 创建一个工程
  2. cargo build cargo run命令
  3. 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的借用有一些必须遵守的规则:

在同一作用域中

  1. 一个或者多个对资源的引用 &T
  2. 只有一个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);
}

应当注意的地方:

  1. struct不支持字段可变性。因此不能在字段上添加 mut修饰
  2. 可变性是绑定的一个属性, 让变量在一段时间内可变

为啥这样设计, 举个例子:

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);

方法语法

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);
  • 创建者模式 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有两种主要的字符串类型:&strString

同 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中是不允许的

---恢复内容结束---

上一篇:【基础机器学习算法】学习目录


下一篇:rust语法