Rust エラー「error: character literal may only contain one codepoint」が発生した場合の対処法

  • 作成日 2022.10.11
  • 更新日 2022.12.02
  • Rust
Rust エラー「error: character literal may only contain one codepoint」が発生した場合の対処法

Rustで、エラー「error: character literal may only contain one codepoint」が発生した場合の対処法を記述してます。Rustのバージョンは1.62.0を使用してます。

環境

  • OS windows11 home
  • rustc 1.62.0

エラー全文

以下のコードで発生

fn main() {

    let mut str: String = "a".to_string();
    
    str.push_str('bc');

    println!("{}", str);

}

エラー全文

error: character literal may only contain one codepoint
 --> sample.rs:5:18
  |
5 |     str.push_str('bc');
  |                  ^^^^
  |
help: if you meant to write a `str` literal, use double quotes
  |
5 |     str.push_str("bc");
  |                  ~~~~

error: aborting due to previous error

原因

文字列には「”(ダブルクォーテーション)」を使用する必要があるため

対処法

「”(ダブルクォーテーション)」を使用する

fn main() {

    let mut str: String = "a".to_string();
    
    str.push_str("bc");

    println!("{}", str); // abc

}