t_wの輪郭

Rust文字列リテラル
&'static strRustの文字列リテラルをbyteとして走査Rustの文字列リテラルを文字として走査+演算子による文字列の結合formatpush_str&strString::from文字列リテラルは&str「文字列リテラルは、高速で効率的」文字列リテラルからString型を生成Rustの文字列リテラルは不変
fn main() {
    for b in "こんにちは".bytes() {
        println!("{}", b);
    }

    println!("");

    let s:&str = "世界";
    for b in s.bytes() {
        println!("{}", b);
    }
}
227
129
147
227
130
147
227
129
171
227
129
161
227
129
175

228
184
150
231
149
140

Stringと&strの結合

fn main() {
    let s1 = String::from("Hello, ");
    let s2:&str = "world!";
    let s3 = s1 + &s2;      // s1はs3へ所有権が移動し、使用できなくなる
    println!("{}", s3);     // Hello, world!
}

format

2022/2/19 6:00:00

文字列といろんなものを結合できるすごい関数マクロ
Displayが実装されていればなんでも結合できる気がする(要検証)


String と Stringの結合

let hello:String = String::from("hello");
let world:String = String::from("world");
let hello_world = format!("{} {}", hello, world);
println!("{}", hello_world);   //hello world

String と 文字列リテラル(&str)の結合

let hello:String = String::from("hello");
let world:&str = "world";
let hello_world = format!("{} {}", hello, world);
println!("{}", hello_world);   //hello world

Stringと整数型(u32)の結合

let hello:String = String::from("hello");
let number:u32 = 1234;
let hello_number = format!("{} {}", hello, number);
println!("{}", hello_number);   //hello 1234