Skip to content

7月1日学习笔记

lirui edited this page Jul 2, 2020 · 2 revisions

Welcome to the RustStudy wiki!

操作系统:Ubuntu 16.04

学习教材:https://kaisery.github.io/trpl-zh-cn/ch01-01-installation.html

1、完成Rust开发环境的搭建;

2、学习cargo的使用

2.1 创建工程 cargo new 工程名

2.2 编译 cargo build

2.3 语法检查 cargo check

2.4 运行 cargo run

3、rust语言是静态类型语言,因此编译时必须知道所有变量的类型,编译器具有自动推导的功能;

4、变量

4.1 变量的定义,mut的使用

4.2 变量的隐藏

4.3 常量的定义

4.4 Demo

  const MAX_POINT: u32 = 100000;

  fn main() {
       //1.变量定义
       //定义变量用let,如果变量没有用mut,那么是不可变的
       //let name: type

       let a = 1;
       println!("a={}",a);
   
       let mut b: u32=1;
       println!("b={}",b);
  
       b = 2;
       println!("b={}",b);
  
       //2.隐藏
       let b: f32 = 1.1;
       println!("b={}",b);
  
       //3.常量
       println!("MAX_POINT={}",MAX_POINT);
       println!("Hello, world!");
   }

5、数据类型

5.1 bool类型

5.2 char类型,在rust里面,char是32位的,和其他语言不一样,尤其和C/C++,在C中,一个char类型是8位的,char可以是一个汉字,也可以是一个字符

5.3 有符号i8,i16,i32,i64,对应无符号u8,u16,u32,u64,浮点型f32,f64

5.4 自适应类型,和平台有关系,有的指针在32位机器上,是32位的,isize(有符号),usize(无符号)

5.5 数组,[Type; size],其中size也是数组类型的一部分

5.6 元组

5.7 Demo

  fn main(){
    //bool
    let is_true: bool = true;
    println!("is_true = {}", is_true);
    
    let is_false: bool = false;
    println!("is_false = {}", is_false);
    
    //char 在rust里面,char是32位的,和其他语言不一样,尤其和C/C++,在C中,一个char类型是8位的,char可以是一个汉字,也可以是一个字符
    let a = 'a';
    println!("a={}",a);
    
    let b = '你';
    println!("b={}",b);
    
    //有符号i8,i16,i32,i64,对应无符号u8,u16,u32,u64,浮点型f32,f64
    let c: i8 = -111;
    println!("c={}", c);
    
    let d: f32 = 0.0009;
    println!("d = {}", d);
    
    //自适应类型,和平台有关系,有的指针在32位机器上,是32位的,isize(有符号),usize(无符号)
    println!("max={}",usize::max_value());
    
    //数组
    //[Type; size],其中size也是数组类型的一部分
    let arr: [u32; 5] = [1, 2, 3, 4, 5];
    let arr1: [u32; 3] = [1,2,3];
    println!("arr[0] = {}", arr[0]);
    
    show(arr1);
    
    //元组
    let tup: (i32, f32, char) = (-3, 3.69, '好');
    println!("{}", tup.0);
    println!("{}", tup.1);
    println!("{}", tup.2);
    
    let (x,y,z) = tup;
    println!("{}", x);
    println!("{}", y);
    println!("{}", z);
    println!("Hello, world!");
 }

 fn show(arr:[u32; 3]){
    println!("------------------------");
    for i in &arr{
        println!("{}",i);
    }
    println!("------------------------");
 }

6、函数,在函数中,语句是执行一些操作,但是不返回值的指令;表达式会计算一些值

  fn other_fun(){
      println!("This is a function");
  }
  
  fn other_fun1(a: i32, b: u32){
      println!("a = {}, b = {}", a, b);
  }
  
  fn other_fun2(a: i32, b: i32)-> i32{
      let result = a + b;
      return result;
  }
 
  fn other_fun3(a: i32, b: i32)-> i32{
      a+b
  }
  
  fn main() {
      other_fun();
  
      let a: i32 = -1;
      let b: u32 = 2;
      other_fun1(a, b);
  
      let c: i32 = 9;
      let r:i32 = other_fun2(a, c);
      println!("r={}", r);
  
      let r2:i32 = other_fun3(a, c);
      println!("r2={}",r2);
  
      //语句是执行一些操作,但是不返回值的指令
      let y = 1; //语句,不返回值
      //let x = (let y = 1)  error
  
      //表达式会计算一些值
      let y = {
          let x = 1;
          x + 1
      };
  
      println!("y={}",y);
  
      println!("Hello, world!");
  }

7、注释 //

8、控制流

8.1 if

8.2 if-else

8.3 if-else if-else

8.4 在let中使用if,在同一分支中类型应该保持一致

8.5 loop

8.6 while

8.7 for

8.8 Demo

  fn main() {
      //if
      let y = 1;
      if y == 1 {
          println!("y=1");
      }
      //if-else
      if y == 1 {
          println!("y = 1");
      }else {
          println!("y != 1");
      }
      //if-else if - else
      println!("+++++++++++++++++");
      let y = 1;
      if y==1 {
          println!("y = 1");
      }else if y == 0 {
          println!("y = 0");
      }else if y == 2 {
          println!("y = 2");
      }else {
          println!("other");
      }
  
  
      //let中使用if
      let condition = true;
      let x = if condition {
          5
      } else {
          6
          //同一分支中类型应该一致
      };
      println!("x = {}",x);
  
      //loop
      let mut counter = 0;
      loop{
          println!("in loop");
          if counter == 10 {
              break;
          }
          counter = counter + 1;
      }
  
      let result = loop {
          counter += 1;
          if counter == 20 {
              break counter*2;
          }
      };
      println!("result = {}", result);
  
      //while
      let mut i = 0;
      while i != 10 {
          i += 1;
      }
      println!("i = {}", i);
  
      //for
  
      let arr:[u32; 5] = [1,2,3,4,5];
      //for element in &arr {  也是可以的  
      for element in arr.iter(){
          println!("element = {}", element);
      }
      println!("Hello, world!");
  }

//从此做个俗人,贪财好色,一身正气~

Clone this wiki locally