collection の読み取り

collection から値を読むときは、「owner を消費する操作」と「借用して Copy 値だけ読む操作」を分けます。get&Vec .T を受け取り、.T: Copy の要素だけを返します。

TESTstdionormalize_newlines
#entry main
#indent 4
#target std

#import "alloc/collections/vec" as *
#import "core/option" as *
#import "core/result" as *
#import "std/test" as *
#import "core/field" as *
#import "core/math" as *

fn has_at %fn &Vec i32 fn i32 fn i32 bool \v\idx\expected:
    match get v idx:
        Option::Some value:
            eq value expected
        Option::None:
            false

fn main %impure fn void i32 \void:
    match filled 3 7:
        Result::Err _e:
            let checks checks_push checks_new Result::Err "vec.filled failed"
            let shown checks_print_report checks
            checks_exit_code shown
        Result::Ok values:
            let n %i32 len &values
            let has0 %bool has_at &values 0 7
            let has2 %bool has_at &values 2 7
            let has3 %bool has_at &values 3 7
            let checks:
                checks_new
                |> checks_push assert_eq_i32 3 n
                |> checks_push assert has0
                |> checks_push assert has2
                |> checks_push assert not has3
            free values;
            let shown checks_print_report checks
            checks_exit_code shown

非 Copy の要素を collection から読みたい場合は、単なる複製ではなく所有権の移動や借用 lifetime を考えます。入門ではまず Copy 要素を borrowed observer で読み取る形を固定します。

On this page