Rust从入门到放弃(1)—— hello,world

安装及环境配置

  • 特点:安全,性能,并发
  • rust源配置
  • RLS安装
  • cargo
    • rust管理工具,该工具可以愉快方便的管理rust工程

      #!/bin/bash
      mkdir learn
      cd learn
      cargo init  ## 该命令会在当前目录下初始化一个
              ## 目录下会出现一个Cargo.toml文件,这是Cargo的配置文件
              ## 还有一个src目录,目录中包含一个main.rs的初始文件
      cargo run   ## 命令会编译并运行程序
      cargo build ## 编译工程

变量与函数

函数定义 fn main(){}

fn main(){
    let world = "world";
    println!("hello, {}!", world);
}
//该例子中可以看出来,变量定义使用关键字 let,字符串格式化中的占位符采用 {}
  • let 关键字用于引入一个变量
  • 占位符 {},在Rust中比较通用,将变量转化为字符串,这里的变量可以是数值或者字符串。
  • 返回值,->
fn main() {
    let x:i32;
    let y:i32;
    x = 10;
    y = 5;
    println!("x = {}, y = {}", x, y);
    println!("add(x, y) = {}", add(x,y));
}

fn add(x:i32, y:i32) ->i32{
    x+y
    // return x+y;
}

变量声明

let val:i32 = 1
  • 在rust中类型定义采用 var: type的形式
fn foo(_x :&'static str) -> &'static str{
    "world"
}
fn main(){
    println!("hello, {}!", foo("bar"));
}
  • 静态字符串变量 &'static str

  • 注意:在rust中,str是关键字,不能用作变量名

let (x,y) = (5, 'a')  
// 类型分别是i32,char
  • 默认情况下,Rust中的一切都是不可变的,也就是说变量定义后不可改变其值,下面这段代码编译不会通过。
let  x:i32 =10;
x = 6;
// ^^^^^ cannot assign twice to immutable variable
  • 如果要改变变量,声明时需要加上mut关键字
let mut x:i32 = 10
x = 6
  • 字符串,Rust中存在两种字符串类型,str和String。

    • &str: 字符串切片,固定大小的,不可变的UTF-8字节序列。
    let x = "hello world!";
    let y:&str = "hahahhahahah";
    • String对象是可变的,可以用String::from初始化该类型
    let x = String::from(“Hello, World”);
    let y: String = String::from(“Isn’t it a wonderful life?");
{
    let mut s1 :&str = "s1 is &str";
    let mut s2 :String = String::from("s2 is String");
    println!("{}, {}", s1, s2);
    // s1 is &str, s2 is String

    s1 = "s1 is changed";
    s2 = String::from("s2 is changed");
    println!("{}, {}", s1, s2);
    // s1 is changed, s2 is changed
}
  • 数字
    • i8 i16 i32 i64
    • u8 u16 u32 u64
    • isize usize
    • f32,f64
  • 数组
let x = [1, 2, 3];
let y: [i32; 3] = [4, 5, 6];
  • 数组不常使用,rust中vector更为常见
let x = vec![1, 2, 3];
let y: Vec<i32> = [4, 5, 6];
  • 元组
    • 有序的、不可变的对象列表

      let x = (5, 'A');
      let y : (i32, char) = (12, 'c');
      let v = x.0 // v == 5
      let a = y.1 // a == 'c'

Rust中的所有权

  • 所有权
    • Rust中,每个变量对绑定到它上的值有所有权。
    • Rust中堆上的每个值都有一个所有者(变量)
    • 当所有者超出作用域时,该值将被丢弃
    // String的长度是可变的,分配在堆中,所以这里的会发生所有权移动。
    // 在Rust中,这个过程称之为移动move,即原本x的值移动到了y上,x失效了。
    fn main(){
        let x = String::from("hello"); // x是"hello"
        let y = x; // y是“hello”,这时x已经失效
        println!("x is {}, f(x) is {}",x, y); // 会出错,因为x失效了。 
    }
  • 借用
    • 任何借用的有效期都不能超过原始所有者的作用域
    • 任何时刻,都能够不可变的借用一个资源许多次
    • 任何时刻,都能够可变的借用一个资源一次

方法&关联函数

  • impl作为实现结构体方法的关键字,方法的输入参数中第一个是self。调用采用.
  • impl范围内,输入参数没有self的方法,即为关联函数。调用使用:
  • 关联函数经常被用作返回一个结构体新实例的构造函数。下面的例子中,Square即为Rect的关联函数。
  • 每个Struct允许多个impl
// 定义一个结构体
struct Rect{
    width: i32,
    length: i32,
}

// 方法
impl Rect{
    fn Area(&self) -> i32{
        self.width * self.length
    }

}

// 关联函数
impl Rect{  
    fn Square(w:i32) -> Rect{
        Rect{width :w,
            length: w,
            }
    }
}

猜你喜欢

转载自www.cnblogs.com/gexin/p/10941227.html