t_wの輪郭

読み込みのみ

let v = vec![100, 32, 57];
for i in &v {
    println!("{}", i);
}
/*
100
32
57
*/

書き込み

let mut v = vec![100, 32, 57];
for i in &mut v {
    *i += 50;
}
for i in &v {
    println!("{}", i);
}
/*
150
82
107
*/

RustのStringを走査

2022/2/19 16:53:00
fn main() {
    let s = String::from("こんにちは世界");
    for b in s.chars() {
        println!("{}", b);
    }
}
こ
ん
に
ち
は
世
界
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
fn main() {
    enum SpreadsheetCell {
        Int(i32),
        Float(f64),
        Text(String),
    }
    
    let row = vec![
        SpreadsheetCell::Int(3),
        SpreadsheetCell::Text(String::from("blue")),
        SpreadsheetCell::Float(10.12),
    ];

    for r in &row {
        match r {
            SpreadsheetCell::Int(i) => println!("{}", i),
            SpreadsheetCell::Text(s) => println!("{}", s),
            SpreadsheetCell::Float(f) => println!("{}", f),
        }
    }
}
3
blue
10.12

https://doc.rust-jp.rs/book-ja/ch08-01-vectors.html#enumを使って複数の型を保持する

Rust一家離散問題

2022/2/19 16:55:00

コード

fn main() {
    for c in "👨‍👩‍👧‍👦".chars() {
        println!("{}", c);
    }
}

もしくは

fn main() {
    let s = String::from("👨‍👩‍👧‍👦");
    for c in s.chars() {
        println!("{}", c);
    }
}

一家離散結果(実行結果)

👨

👩

👧

👦
let a = [10, 20, 30, 40, 50];

for element in a.iter() {
    println!("the value is: {}", element);
}
/*
the value is: 10
the value is: 20
the value is: 30
the value is: 40
the value is: 50
*/