当前位置:   article > 正文

Rust入门篇 &mut_rust &self mut

rust &self mut

Rust入门篇


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

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

Hello World

  1. 使用 cargo new projectName --bin 创建一个工程
  2. cargo buildcargo run命令
  3. cargo配置文件: 工程下的 Cargo.toml 文件

所有权

变量绑定

变量绑定有它们所绑定的的值的所有权。这意味着当一个绑定离开作用域,它们绑定的资源就会被释放。

  1. let a = vec![21]; // let声明一个变量绑定,非变量
  2. a.push(90); // error: cannot borrow immutable local variable `a` as mutable 对象默认是immutable
  3. let a = 'x'; // a 重新绑定一个对象
  4. a = 'a'; // error: re-assignment of immutable variable `a`
  • 拓展:Rust是一门静态隐式类型的语言。

    类型在编译时推导, 类似也c++11的auto特性

移动语义

Rust确保了对于任何给定的资源都只有一个绑定与之对应。

  1. let a = vec![1, 2];
  2. let b = a; // 将a绑定的对象所有权交给b.
  3. println!("{}", a[0]); // error: use of moved value: `a`

拷贝语义

同其他C-style语言一样, Rust的基本类型具有copy语义

  1. let a = 32;
  2. let b = a;
  3. println!("{}", a); // 不报错

借用(Borrowing)

  • 引子:
  1. fn main() {
  2. fn fn1(arg: Vec<i32>) -> u32 { // 函数的定义格式...
  3. 21 // 表达式可以返回一个值
  4. }
  5. let a = vec![21, 32];
  6. fn1(a); // 将a绑定的对象所有权传入函数中...
  7. println!("{}", a[0]); // use of moved value: `a`
  8. }

如何解决这个问题?

1. 使用 borrowing
  1. fn main() {
  2. fn fn1(arg: &Vec<i32>) -> u32 { // 需传入一个引用
  3. 21
  4. }
  5. let a = vec![21, 32];
  6. fn1(&a); // 传入&T类型,一个引用类型
  7. println!("{}", a[0]);
  8. }

上述的借用都是immutable借用类型, 还有&mut类型。
Rust的借用有一些必须遵守的规则:
在同一作用域中

  1. 一个或者多个对资源的引用 &T
  2. 只有一个mutable引用 &mut

原因: 在编译时避免数据竞争...

  • 例子:
  1. let mut x = 5;
  2. let y = &mut x;
  3. *y += 1;
  4. println!("{}", x); // cannot borrow `x` as immutable because it is also borrowed as mutable

不过,解决这个问题的方法是... 缩小y的作用范围:

  1. let mut x = 5;
  2. {
  3. let y = &mut x;
  4. *y += 1;
  5. }
  6. println!("{}", x);
2. 对象克隆
  1. fn main() {
  2. fn fn1(arg: Vec<i32>) -> u32 {
  3. 21
  4. }
  5. let a = vec![21, 32];
  6. fn1(a.clone()); // 将a的副本传入即可
  7. println!("{}", a[0]); // use of moved value: `a`
  8. }

生命周期

在Rust中,引用必须与它引用的资源存活得一样长!

  如下两例子:

  1. let r : &i32;
  2. {
  3. let a = 32;
  4. r = &32; // error: borrowed value does not live long enough
  5. }
  6. println!("{}", r);
  1. let r : &i32;
  2. let x = 78;
  3. r = &x; // error: `x` does not live long enough
  • 注意在Rust中 生命周期 这概念是与引用/借用紧密关联的
  • 它定义了引用有效的作用域。

前面见过的有一个引用类型作为参数的函数,之所以没有看到声明周期这东东。 是因为声明周期省略造成的错觉。

我们可以以 implicit 或者 explicit 的方式来定义一个函数:

  1. // implicit
  2. fn foo(x: &i32) -> &i32{
  3. }
  4. // explicit
  5. fn bar<'a>(x: &'a i32) -> &'a i32{
  6. }

此外,结构体(struct)也拥有生命周期。
接下来解决struct后再继续...

类型

结构体

一个简单的struct:

  1. struct Point {
  2. x: i32, // Note: 逗号作为分隔符
  3. y: i32,
  4. }
  5. fn main() {
  6. let origin = Point { x: 0, y: 0 };
  7. println!("The origin is at ({}, {})", origin.x, origin.y);
  8. }

应当注意的地方:

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

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

  1. struct Point {
  2. x: i32,
  3. y: i32,
  4. }
  5. fn main() {
  6. let mut point = Point { x: 0, y: 0 };
  7. point.x = 5;
  8. let point = point; // this new binding can’t change now
  9. point.y = 6; // this causes an error
  10. }
生命周期 · 续

当结构体中具有引用类型的属性时, 结构体就需要使用显示的生命周期。
错误示例:

  1. struct Foo {
  2. x: &i32, // error: missing lifetime specifier
  3. }

正确的写法:

  1. struct Foo<'a> {
  2. x: &'a i32,
  3. }
  4. fn main() {
  5. let y = &5; // 等价于 `let _y = 5; let y = &_y;`
  6. let f = Foo { x: y };
  7. println!("{}", f.x);
  8. }

为什么Foo需要一个生命周期? 因为我们需要确保Foo中的任何引用不能比它包含的 i32 的引用活的更久。

impl

使用impl在Foo中定义一个方法:

  1. fn main() {
  2. let y = &5;
  3. let f = Foo { x: y };
  4. println!("{}", f.x());
  5. }
  6. struct Foo<'a> {
  7. x: &'a i32,
  8. }
  9. impl<'a> Foo<'a> { // 标点符号吓死人系列...
  10. fn x(&self) -> &'a i32 { self.x }
  11. }

'a 就是用来赋予作用域一个名字。
下面介绍一个特殊的命名作用域:

  • 'static
    • 在Rust中最常见的: let x: &'static str = "Hello, world.";
    1. static FOO: i32 = 10; // 定义一个常量
    2. let x: &'static i32 = &FOO;
    3. println!("{}", *x);
方法语法
  1. struct Circle {
  2. x: f64,
  3. y: f64,
  4. radius: f64,
  5. }
  6. impl Circle {
  7. fn area(&self) -> f64 {
  8. std::f64::consts::PI * (self.radius * self.radius)
  9. }
  10. }
  11. fn main() {
  12. let c = Circle { x: 0.0, y: 0.0, radius: 2.0 };
  13. println!("{}", c.area());
  14. }
方法的第一个参数比较特殊。它有3种变体: `self``&self``&mut self`。 通常使用后两种! 当方法只是读取struct中的数据时使用`&self`。 若要修改数据则使用`&mut self`
  • 关联函数

    不带self参数的方法就是关联函数。 这是一个Rust代码中非常常见的模式。

    1. impl Circle {
    2. fn new(x: f64, y: f64, radius: f64) -> Circle {
    3. Circle {
    4. x: x,
    5. y: y,
    6. radius: radius,
    7. }
    8. }
    • 关联函数的调用: `let c = Circle::new(0.0, 0.0, 2.0);

  • 创建者模式 Builder Pattern

枚举

C不同,Rust的枚举可携带数据.... 看个例子

  1. enum Message {
  2. Quit,
  3. ChangeColor(i32, i32, i32),
  4. Move {x: i32, y: i32},
  5. Write(String),
  6. }
  7. // 使用 match 来实现类型的转换
  8. fn process_message(msg: Message) -> i32{
  9. match msg { // match所有分支返回类型必须一致
  10. Message::Quit => 32, // 逗号隔开
  11. Message::ChangeColor(r,g,b) => r+g+b,
  12. Message::Move{x: x1, y: y1} => x1 + y1,
  13. Message::Write(s) => s.trim().parse().ok().expect("parse error!"),
  14. }
  15. }
  16. fn main() {
  17. let a = Message::Quit;
  18. let b = Message::ChangeColor(1, 2, 3);
  19. let c = Message::Move{x: 32, y: -32};
  20. let d = Message::Write("88".to_string());
  21. println!("{}", process_message(a));
  22. println!("{}", process_message(b));
  23. println!("{}", process_message(c));
  24. println!("{}", process_message(d));
  25. }

匹配和模式

  1. let x = 5;
  2. match x {
  3. 1 => println!("one"),
  4. 2 => println!("two"),
  5. 3 => println!("three"),
  6. 4 => println!("four"),
  7. 5 => println!("five"),
  8. _ => println!("something else"),
  9. }

Rust编译器检查穷尽性,要求对每一个枚举的变量都有一个匹配分支。如果你忽略了一个,除非你用_否则它会给你一个编译时错误。

模式

在匹配语句中使用到:

  1. let my_number = 8;
  2. match my_number {
  3. 0 => println!("zero"),
  4. 1 | 2 => println!("one or two"), // Multiple patterns
  5. 3 ... 10 => println!("three to ten"), // Ranges
  6. _ => println!("something else")
  7. }

解构: 对于复合数据类型, 可以在模式中进行解析

  1. struct Point {
  2. x: i32,
  3. y: i32,
  4. }
  5. let origin = Point { x: -9, y: 0=77 };
  6. match origin {
  7. Point { x, y } => println!("({},{})", x, y),
  8. }
  9. // 解析部分值 使用 .. 来忽略部分或所有值
  10. match origin {
  11. Point { x, .. } => println!("x is {}", x),
  12. }

忽略绑定

  1. fn fn1() -> (i32, i32) {
  2. (33, 43)
  3. }
  4. let (i, _ ) = fn1(); // 只绑定fn1第一个值, 忽略第二个值的绑定
  5. 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
越界访问
  1. let v = vec![32, 43];
  2. println!("{:?}", v[3]); // 运行时 thread '<main>' panicked at 'index out of bounds
迭代
  1. let mut v = vec![1, 2, 3, 4, 5];
  2. for i in &v {
  3. println!("A reference to {}", i);
  4. }
方法
  1. let v = vec![43, 54, 65]; // v: Vec<i32>
  2. // 数组长度
  3. println!("{:?}", v.len());

字符串

Rust有两种主要的字符串类型:&strString

同 C-style 系, let greeting = "Hello there."; // greeting: &'static str &str编译后存储在程序中, 在运行期间一直存在。

String则不同,是一个在堆上分配的字符串。这个字符串可以增长,并且也保证是UTF-8编码的。

  1. let mut s = "Hello".to_string(); // mut s: String
  2. println!("{}", s);
  3. s.push_str(", world.");
  4. println!("{}", s);

String可以通过一个&强制转换为&str

  1. let tmp = "鬼".to_string();
  2. let s = "什么".to_string() + &tmp; // String + str => String
  3. println!("{:?}", s);

题外话: 被恶心到了... str + str 和 String + String 是不被允许的
不懂为啥这样设计

Note : 由于let s = "hello";中"hello"是一个UTF-8编码的字符串,故不能直接用索引来访问字符串的元素。 编码扫盲篇

关于Rust的字符串(如"hello"), 就好像你在ipython中输入:

注意这里使用的是 python2.7

  1. > a = '严'
  2. > a
  3. > '\xe4\xb8\xa5'
  4. > len(a)
  5. > 3

在python中你可以使用a[2]来访问a指向的str。 但这在Rust中是不允许的

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/556659
推荐阅读
相关标签
  

闽ICP备14008679号