Skip to content
On this page

原生类型2: exercises/primitive_types/primitive_types2.rs

题目

rust
// primitive_types2.rs
//
// Fill in the rest of the line that has code missing! No hints, there's no
// tricks, just get used to typing these :)
//
// Execute `rustlings hint primitive_types2` or use the `hint` watch subcommand
// for a hint.

// I AM NOT DONE

fn main() {
    // Characters (`char`)

    // 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 // Finish this line like the example! What's your favorite character?
    // Try a letter, try a number, try a special character, try a character
    // from a different language than your own, try an emoji!
    if your_character.is_alphabetic() {
        println!("Alphabetical!");
    } else if your_character.is_numeric() {
        println!("Numerical!");
    } else {
        println!("Neither alphabetic nor numeric!");
    }
}

填充缺失的代码行。

题目解析

注释里面提示我们,要注意'单引号和"双引号的区别,单引号括起来的为char类型。

下面第二段注释提醒我们,根据示例完成缺失的行,你最喜欢字符是什么?尝试一下字母、数字、特殊字符、其他语言的字符,尝试一下emoji表情。

下面几种情况你都可以试一下:

rust
let your_character = 'a';
let your_character = '1';
let your_character = '?';
let your_character = '';
let your_character = '🦀';

参考资料

Powered by VitePress