t_wの輪郭

Feedlyでフォローするボタン

format

2022/2/19 15:00:00

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


StringStringの結合

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

to_string()

2022/2/19 14:35:00

Display特性のメソッド
Stringを生成する

fn main() {
    let s : &str = "Hello, world";
    let string1 = s.to_string();
    let string2 = "world hello".to_string();
    let i = 1;
    let string3 = i.to_string();
    let string4 = 2.to_string();
    println!("{}", string1);      //Hello, world
    println!("{}", string2);      //world hello
    println!("{}", string3);      //1
    println!("{}", string4);      //2
}