t_wの輪郭

コレクションRust文字列

String

2022/2/18 12:34:00

Rustの文字列オブジェクト
文字のコレクション

linesto_lowercasecontentsRustのStringを走査StringはVec<u8>のラッパ

lines

2022/2/23 23:56:00

文字列を行ごとに繰り返すメソッド

let s = String::from("\
Rust:
safe, fast, productive.
Pick three.");
for line in s.lines() {
    println!("{}", line);
}

to_lowercase

2022/2/23 23:55:00

文字列を小文字にするメソッド

assert_eq!("Rust".to_lowercase(), "rust");
assert_eq!(String::from("Rust").to_lowercase(), String::from("rust"));

contents

2022/2/23 23:37:00

文字列にクエリ文字列を含むか確認するメソッド

let s = String::from("safe, fast, productive.");
println!("{}", s.contains("duct"));   //true
let s = "safe, fast, productive.";
println!("{}", s.contains("duct"));   //true

RustのStringを走査

2022/2/19 7:53:00
fn main() {
    let s = String::from("こんにちは世界");
    for b in s.chars() {
        println!("{}", b);
    }
}
こ
ん
に
ち
は
世
界

String&strの結合

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

Stringu32の結合

+演算子では不可能。formatを使おう。

fn main() {
    let s1 = String::from("Hello, ");
    let s2 = 1234;
    let s3 = s1 + &s2;      // error!!!
    println!("{}", s3);     
}
error[E0308]: mismatched types
 --> src\main.rs:4:19
  |
4 |     let s3 = s1 + &s2;      // error!!!
  |                   ^^^ expected `str`, found integer
  |
  = note: expected reference `&str`
             found reference `&{integer}`