ブール値
fn main(){ let t = true; let f: bool = false; println!("{} {}", t, f); }
キャラクタ
fn main(){ let c = 'z'; let n:char = '0'; let jp = 'あ'; println!("{} {} {}", c, n, jp); }
b’~’で囲んだASCII文字はバイト文字として扱われる
let chb = b’a’;
let ch = ‘a’;
let chjp = ‘あ’;
文字列
RustにはstrとStringの2種類がある。String型はプリミティブなデータ型ではない。strは文字列スライトという
let msg:&str = “Hello, Dogs!”;
let s = &msg[2..5];
fn main(){ let msg:&str = "Hello, Dogs!"; println!("msg.len()={}", msg.len()); println!("msg={}", msg); println!("msg={}", &msg[2..5]); }
fn main(){ let dog = "ポチ"; let msg = format!("{} ({}歳)", dog, 6); println!("{}", msg); }
fn main(){ let mut msg:String = String::from("Hello"); msg = msg + " Rust!"; println!("{}", msg); }
push_str, insert_str, +がある
fn main(){ let mut msg = String::from("ポチ"); msg.push_str("Good Dog"); println!("{}", msg); }
fn main(){ let mut s = String::from("ポチ"); s.insert_str(0, "Hello"); println!("{}", s); }
fn main(){ let mut msg = String::from("ポチ"); msg = msg + "Good Dog"; println!("{}", msg); }