やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

Rustのデータ型(文字)

 char型。

成果物

コード

fn main() {
    let c1 = 'A';
    let c2: char = 'B';
    println!("{} {}", c1, c2);
    if c1 == c2 { println!("c1 == c2"); }
    else { println!("c1 != c2"); }
}

エラー例

2字以上入れる

error: character literal may only contain one codepoint
 --> main.rs:8:14
  |
8 |     let c3 = 'AB';
  |              ^^^^

ダブルクォート

error[E0308]: mismatched types
 --> main.rs:9:20
  |
9 |     let c4: char = "A";
  |                    ^^^ expected char, found reference
  |
  = note: expected type `char`
             found type `&'static str`

 Rustはクォートの種類によって型が変わるらしい。

クォート
シングル' char
ダブル" &'static str

参考

対象環境

$ uname -a
Linux raspberrypi 4.19.42-v7+ #1219 SMP Tue May 14 21:20:58 BST 2019 armv7l GNU/Linux

前回まで