kumuのつぶやき

フロントエンド勉強中の学生のただつぶやき

Rust 文字列

文字列型

  1. String
  2. &str

// ヒープに割り当てる
let a = String::from("test");
let a = "test".to_string();
let a: String = "test".into();

// read only memory上に割り当てられた文字列への参照
let b: &'static str = "test";
// &str
let b = "test";


リテラル

// "test"
let byte_escape = "\x74\x65\x73\x74";
// [116, 101, 115, 116]
let byte_escape = b"\x74\x65\x73\x74";

// unicode "א"
let ucode = "\u{05D0}";

// raw string "\x74 \u{05D0}"
let raw_str = r"\x74 \u{05D0}";
// 引用符使う場合
let raw_str = r#" "test" "#;

// バイト文字列 [116, 101, 115, 116]
let a: &[u8; 4] = b"test";