zoukankan      html  css  js  c++  java
  • Rust Book 学习记录(Nightly)

    2.1 安装

    $ curl -L https://static.rust-lang.org/rustup.sh | sudo sh

    2.2 Hello, world!

    创建项目

    $ mkdir ~/projects
    $ cd ~/projects
    $ mkdir hello_world
    $ cd hello_world

    创建 main.rs,当前目录结构

    projects/
    └── hello_world
        └── main.rs

    编写代码

    fn main() {
        println!("Hello, world!");  
    }

    在终端编译 main.rs

    $ rustc main.rs    // 编译

    现在我们的工程目录变成了这样

    projects/
    └── hello_world
        ├── main    // 可执行文件
        └── main.rs

     执行 main,可以看到输出的 “Hello, world!”。

    $ ./main # or main.exe on Windows // 运行生成的 main 执行文件
    Hello, world! // 输出的 Hello, world!

     println!() 是 Rust 的宏,只要是看到了叹号,就是一个代替普通函数的宏。

    2.3 Cargo!

    Cargo 是帮助管理 Rust 工程的工具。

    进入你的工程目录,创建 src 文件夹,并将之前的 main.rs 移动到文件夹内

    $ mkdir src
    $ mv main.rs src/main.rs

    创建 Cargo.toml 文件

    [package]     // package 部分告诉 Cargo 程序的信息(元数据)
    
    name = "hello_world"
    version = "0.0.1"
    authors = [ "Your name <you@example.com>" ]
    
    [[bin]]    // 告诉 Cargo 生成一个二进制可执行文件
    
    name = "hello_world"    // 可执行文件的名字

    执行编译命令,运行编译出的可执行文件

    $ cargo build    // 编译代码文件
       Compiling hello_world v0.0.1 (file:///home/yourname/projects/hello_world)
    $ ./target/hello_world    // 运行生成的可执行文件
    Hello, world!    // 输出的信息

    当前工程目录

    projects/
    └── hello_world
        ├── Cargo.lock
        ├── Cargo.toml
        ├── main    // 之前用 rustc 生成的可执行文件
        ├── src
        │   └── main.rs
        └── target    // 执行 $ cargo build 命令后生成的文件夹
            ├── build
            ├── deps
            ├── examples
            ├── hello_world    // 和 main 一样的可执行文件
            └── native

    2.4 变量

    声明一个变量 x,并赋值为 5

    fn main() {
        let x = 5;
    
        // 也可以这样,官方叫做 “pattern matching”
        let (y, z) = (1, 2);
    }

    变量默认是不可变的(immutable)

    let x = 5;
    x = 10; // 这样做会报错!!!

     如果希望它是可变的,需要使用 “mut”(即 mutable 的缩写)

    let mut x = 5;
    x = 10; // 这样可以!!!

    也可以这样声明

    fn main() {
        let x: i32; // 如果不加“i32”,默认就是“i32”的格式。
    }

    如果直接“println!()”了没有被赋值的变量会报错,像这样

    fn main() {
        let x: i32;
    
        println!("The value of x is: {}", x);  
    }
    
    // 会报“使用了没有初始化的变量”(use of possibly uninitialized variable: 'x')的错误

    “println!()”中被打印的字符串和变量之间使用逗号分隔,使用一组花括号“{}”代表你的“x”的插入位置

    更多输出格式访问:http://doc.rust-lang.org/std/fmt/

    2.5 if

    fn main() {
        let x = 5;
    
        if x == 5 {
            println!("x is five!");
        }
    
        // 或者这样
    
        if x == 5 {
            println!("x is five!");
        } else {
            println!("x is not five :(");
        }
    }

    Rust 中的 if 还可以这样写

    let x = 5;
    
    // 如果 x 等于 5,就将 y 赋值为 10,否则 y 值为 15
    let y = if x == 5 {
        10
    } else {
        15
    };
    
    // 还可以这样写
    let y = if x == 5 { 10 } else { 15 };
    
    // 注意:默认的 x 和 y 的类型是 i32 哦,即 32 位整数!
    // 如果你给花括号中的 10 和 15 加上分号就错了哦!像这样……
    let y = if x == 5 { 10; } else { 15; };

    在其他编程语言中,if 是语句;在 Rust 中,if 是表达式。

    2.6 函数

    fn main() { // 函数以“fn” 开头
        // 调用下面的那个函数
        print_number(5);
    }
    
    /// 一个名为“print_number”函数
    /// 圆括号里面的 x: i32 是函数的参数
    /// 一个函数可以有多个参数,看你的需求了
    fn print_number(x: i32) {
        println!("x is {}", x);  
    }

    这个例子运行后将会输出 x is 5。

    // 正如上面说的,函数的参数可以多个
    fn print_sum(x: i32, y: i32) {
        println!("sum is {}", x + y);
    }
    
    // 调用 print_num(5, 6); 算出 5+6 的值
    // 运行会输出 sum is 11

    下面这样写会报错,即参数没有指定类型会报错

    fn print_sum(x, y) { // 参数没有指定类型,错误!
        println!("sum is {}", x + y);
    }

    关于函数的返回值,看代码!

    fn main() {
        // 调用 add_one 函数
        println!("3 + 1 = {}", add_one(3));
    }
    
    fn add_one(x: i32) -> i32 { // 箭头(->)后面的是返回值的类型
        x + 1 // 返回值后面不能加分号哦,会报错的!
    }

    以上例子会输出 3 + 1 = 4

    其他的返回方式

    fn foo(x: 32) -> i32 {
        if x < 5 { return x; } // 如果 x 小于 5,提前使用 return 返回
    
        x + 1
    }
    
    // 当然也可以使用 return 返回 x + 1 ,不过官方不推荐
    fn foo(x: 32) -> i32 {
        if x < 5 { return x; } // 如果 x 小于 5,提前使用 return 返回
    
        return x + 1; // 如果使用 return 结尾需要加上分号哦!
    }

    加上之前的 if,你也可以这样写

    fn foo(x: i32) -> i32 {
        if x < 5 {
            x
        } else {
            x + 1
        }
    }

    2.7 注释

    // 普通注释
    
    /// 文档注释
    
    /// # 文档注释支持 Markdown
    /// ## 可以使用 rustdoc 工具生成 html 文档
    /// ### http://doc.rust-lang.org/rustdoc.html
  • 相关阅读:
    hashlib模块
    configparser模块
    xml模块和shelve模块
    json与pickle模块
    3/30
    os模块
    sys模块
    shutil模块
    random模块
    2月书单《编码隐匿在计算机软硬件背后的语言》 13-16章
  • 原文地址:https://www.cnblogs.com/shushengfu/p/learn_rust_book_nightly.html
Copyright © 2011-2022 走看看