rustlings 学习笔记 -- exercises/04_primitive_types

8 阅读4分钟

exercises/04_primitive_types

primitive_types1.rs - 布尔类型

问题代码

// Booleans (`bool`)

fn main() {
    let is_morning = true;
    if is_morning {
        println!("Good morning!");
    }

    // TODO: Define a boolean variable with the name `is_evening` before the `if` statement below.
    // The value of the variable should be the negation (opposite) of `is_morning`.
    // let …
    if is_evening {
        println!("Good evening!");
    }
}

解题要点

  • 错误原因: 缺少 is_evening 变量定义,需要在 if 语句前声明
  • 解决方法: 定义布尔变量,值为 is_morning 的相反值

正确写法

// Booleans (`bool`)

fn main() {
    let is_morning = true;
    if is_morning {
        println!("Good morning!");
    }

    let is_evening = !is_morning;
    if is_evening {
        println!("Good evening!");
    }
}

primitive_types2.rs - 字符类型

问题代码

// Characters (`char`)

fn main() {
    // Note the _single_ quotes, these are different from the double quotes
    // you've been seeing around.
    let my_first_initial = 'C';
    if my_first_initial.is_alphabetic() {
        println!("Alphabetical!");
    } else if my_first_initial.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }

    // TODO: Analogous to the example before, declare a variable called `your_character`
    // below with your favorite character.
    // Try a letter, try a digit (in single quotes), try a special character, try a character
    // from a different language than your own, try an emoji 😉
    // let your_character = '';

    if your_character.is_alphabetic() {
        println!("Alphabetical!");
    } else if your_character.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }
}

解题要点

  • 错误原因: 缺少 your_character 变量定义,需要在 if 语句前声明
  • 解决方法: 定义一个字符变量,使用单引号包围字符

正确写法

// Characters (`char`)

fn main() {
    // Note the _single_ quotes, these are different from the double quotes
    // you've been seeing around.
    let my_first_initial = 'C';
    if my_first_initial.is_alphabetic() {
        println!("Alphabetical!");
    } else if my_first_initial.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }

    let your_character = '😉';
    if your_character.is_alphabetic() {
        println!("Alphabetical!");
    } else if your_character.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }
}

primitive_types3.rs - 数组类型

问题代码

fn main() {
    // TODO: Create an array called `a` with at least 100 elements in it.
    // let a = ???

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
        panic!("Array not big enough, more elements needed");
    }
}

解题要点

  • 错误原因: 缺少数组 a 的定义,需要创建至少 100 个元素的数组
  • 解决方法: 使用数组语法创建包含 100 个或更多元素的数组

正确写法

fn main() {
    let a = [0; 100];

    if a.len() >= 100 {
        println!("Wow, that's a big array!");
    } else {
        println!("Meh, I eat arrays like that for breakfast.");
        panic!("Array not big enough, more elements needed");
    }
}

primitive_types4.rs - 数组切片

问题代码

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    #[test]
    fn slice_out_of_array() {
        let a = [1, 2, 3, 4, 5];

        // TODO: Get a slice called `nice_slice` out of the array `a` so that the test passes.
        // let nice_slice = ???

        assert_eq!([2, 3, 4], nice_slice);
    }
}

解题要点

  • 错误原因: 缺少 nice_slice 切片的定义,需要从数组中提取特定范围的元素
  • 解决方法: 使用切片语法 [start..end] 创建数组切片

正确写法

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    #[test]
    fn slice_out_of_array() {
        let a = [1, 2, 3, 4, 5];

        let nice_slice = &a[1..4];

        assert_eq!([2, 3, 4], nice_slice);
    }
}

primitive_types5.rs - 元组解构

问题代码

fn main() {
    let cat = ("Furry McFurson", 3.5);

    // TODO: Destructure the `cat` tuple in one statement so that the println works.
    // let /* your pattern here */ = cat;

    println!("{name} is {age} years old");
}

解题要点

  • 错误原因: 缺少元组解构,需要将元组的值提取到变量中
  • 解决方法: 使用模式匹配语法解构元组

正确写法

fn main() {
    let cat = ("Furry McFurson", 3.5);

    let (name, age) = cat;

    println!("{name} is {age} years old");
}

primitive_types6.rs - 元组索引

问题代码

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    #[test]
    fn indexing_tuple() {
        let numbers = (1, 2, 3);

        // TODO: Use a tuple index to access the second element of `numbers`
        // and assign it to a variable called `second`.
        // let second = ???;

        assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
    }
}

解题要点

  • 错误原因: 缺少元组索引访问,需要获取元组的第二个元素
  • 解决方法: 使用点号和索引访问元组元素

正确写法

fn main() {
    // You can optionally experiment here.
}

#[cfg(test)]
mod tests {
    #[test]
    fn indexing_tuple() {
        let numbers = (1, 2, 3);

        let second = numbers.1;

        assert_eq!(second, 2, "This is not the 2nd number in the tuple!");
    }
}

知识点汇总

基础类型

  • 布尔类型: 使用 bool 表示真值,支持逻辑运算 !&&||
  • 字符类型: 使用单引号 char 表示 Unicode 字符,支持各种语言和表情符号
  • 类型方法: 字符类型有 is_alphabetic()is_numeric() 等方法

数组类型

  • 数组定义: 使用 [T; N] 语法创建固定大小数组,如 [0; 100]
  • 数组长度: 使用 .len() 方法获取数组元素个数
  • 数组切片: 使用 &array[start..end] 创建数组切片引用

元组类型

  • 元组定义: 使用括号 (value1, value2, ...) 创建元组
  • 元组解构: 使用 let (var1, var2) = tuple; 解构元组
  • 元组索引: 使用 tuple.index 访问元组元素,索引从 0 开始