╭ test_for.zig ─────────────────────────────────────────────────╮ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ │ ╰───────────────────────────────────────────────────────────────╯
╭ find file ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╮ ╭────╮ ╭ clone ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╮ │ │ │http│ ssh │ │ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─╯ ╰────╯ ╰ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ╯
.. Assembly Syntax Explained.zig TopLevelFields.zig addWithOverflow_builtin.zig anonymous_struct_name.zig assign_undefined.zig bad_default_value.zig base64.zig build.zig build_c.zig build_object.zig catch.zig catch_err_return.zig change_active_union_field.zig checking_null_in_zig.zig cli_allocation.zig comments.zig compile-time_duck_typing.zig compile_variables.zig compiler_generated_function.zig constant_identifier_cannot_change defer_unwind.zig destructuring_arrays.zig destructuring_block.zig destructuring_mixed.zig destructuring_return_value.zig destructuring_to_existing.zig destructuring_vectors.zig doc_comments.zig entry_point.zig enum_export.zig enum_export_error.zig error_return_trace.zig error_union_parsing_u64.zig export_any_symbol_name.zig export_builtin.zig export_builtin_equivalent_code.zi fibonacci_comptime_infinite_recur float_literals.zig float_mode_exe.zig float_mode_obj.zig float_special_values.zig generic_data_structure.zig grammar.peg handle_error_with_catch_block.zig hello.zig hello_again.zig identifiers.zig inline_assembly.zig inline_call.zig inline_prong_range.zig integer_literals.zig invalid_doc-comment.zig lang.CallModifier struct.zig libc_export_entry_point.zig math.zig math_add.zig mathtest.zig multiline_string_literals.zig mutable_var.zig not_atomic_cmpxchgStrong.zig null.zig optional_integer.zig packed_struct_mmio.zig panic_handler.zig poc_printValue_fn.zig poc_print_fn.zig print.zig print_comptime-known_format.zig redundant_fqn.zig result_location_interfering_with_ result_type_propagation.zig runtime_divExact_remainder.zig runtime_division_by_zero.zig runtime_incorrect_pointer_alignme runtime_index_out_of_bounds.zig runtime_invalid_cast.zig runtime_invalid_cast_truncate.zig runtime_invalid_enum_cast.zig runtime_invalid_error_code.zig runtime_invalid_error_set_cast.zi runtime_invalid_null_pointer_cast runtime_out_of_bounds_float_to_in runtime_overflow.zig runtime_reaching_unreachable.zig runtime_remainder_division_by_zer runtime_shlExact_overflow.zig runtime_shrExact_overflow.zig runtime_unwrap_error.zig runtime_unwrap_null.zig runtime_vs_comptime.zig runtime_wrong_union_field_access. sentinel-terminated_pointer.zig single_value_error_set.zig single_value_error_set_shortcut.z slicing_by_length.zig stack_trace.zig std_options.zig string_literals.zig struct_default_field_values.zig struct_default_value.zig struct_name.zig test_TypeOf_builtin.zig test_aligned_struct_fields.zig test_allocator.zig test_allowzero.zig test_ambiguous_coercion.zig test_anonymous_struct.zig test_anonymous_union.zig test_arrays.zig test_assertion_failure.zig test_basic_slices.zig test_bitOffsetOf_offsetOf.zig test_blocks.zig test_call_builtin.zig test_coerce_error_subset_to_super test_coerce_error_superset_to_sub test_coerce_large_to_small.zig test_coerce_optional_wrapped_erro test_coerce_optionals.zig test_coerce_slices_arrays_and_poi test_coerce_to_error_union.zig test_coerce_tuples_arrays.zig test_coerce_unions_enums.zig test_compileLog_builtin.zig test_comptime_call_extern_functio test_comptime_divExact_remainder. test_comptime_division_by_zero.zi test_comptime_evaluation.zig test_comptime_incorrect_pointer_a test_comptime_index_out_of_bounds test_comptime_invalid_cast.zig test_comptime_invalid_cast_trunca test_comptime_invalid_enum_cast.z test_comptime_invalid_error_code. test_comptime_invalid_error_set_c test_comptime_invalid_null_pointe test_comptime_max_with_bool.zig test_comptime_mismatched_type.zig test_comptime_out_of_bounds_float test_comptime_overflow.zig test_comptime_pointer_conversion. test_comptime_pointers.zig test_comptime_reaching_unreachabl test_comptime_remainder_division_ test_comptime_shlExact_overflow.z test_comptime_shrExact_overflow.z test_comptime_unreachable.zig test_comptime_unwrap_error.zig test_comptime_unwrap_null.zig test_comptime_variables.zig test_comptime_wrong_union_field_a test_defer.zig test_defining_variadic_function.z test_empty_block.zig test_enum_literals.zig test_enums.zig test_error_union.zig test_exhaustive_switch.zig test_expression_ignored.zig test_failed_int_to_float_coercion test_fibonacci_comptime_overflow. test_fibonacci_comptime_unreachab test_fibonacci_recursion.zig test_field_builtin.zig test_fn_reflection.zig test_fn_type_inference.zig ╭─────────────────────────────────╮ │test_for.zig │ ╰─────────────────────────────────╯ test_for_nested_break.zig test_functions.zig test_global_assembly.zig test_hasDecl_builtin.zig test_if.zig test_if_optionals.zig test_illegal_behavior.zig test_incorrect_pointer_alignment. test_inferred_error_sets.zig test_inline_else.zig test_inline_for.zig test_inline_switch.zig test_inline_switch_union_tag.zig test_inline_while.zig test_intCast_builtin.zig test_int_to_float_coercion.zig test_integer_pointer_conversion.z test_integer_widening.zig test_invalid_defer.zig test_labeled_break.zig test_locally_scoped_global_variab test_merging_error_sets.zig test_misaligned_pointer.zig test_missized_packed_struct.zig test_multidimensional_arrays.zig test_namespace-level_comptime_exp test_namespace_level_variables.zi test_namespaced_variable.zig test_no_op_casts.zig test_noreturn.zig test_noreturn_from_exit.zig test_null_terminated_array.zig test_null_terminated_slice.zig test_null_terminated_slicing.zig test_opaque.zig test_optional_pointer.zig test_optional_type.zig test_overaligned_packed_struct.zi test_packed_struct_backing_int.zi test_packed_struct_equality.zig test_packed_struct_field_address. test_packed_structs.zig test_packed_union_equality.zig test_pass_by_reference_or_value.z test_peer_type_resolution.zig test_pointer_arithmetic.zig test_pointer_casting.zig test_pointer_coerce_const_optiona test_pointer_to_non-byte_aligned_ test_print_too_many_args.zig test_reduce_builtin.zig test_round_builtin.zig test_scopes.zig test_sentinel_mismatch.zig test_setEvalBranchQuota_builtin.z test_setRuntimeSafety_builtin.zig test_shadowing.zig test_shuffle_builtin.zig test_simple_union.zig test_single_item_pointer.zig test_slice_bounds.zig test_slices.zig test_splat_builtin.zig test_src_builtin.zig test_string_literal_to_const_slic test_string_literal_to_slice.zig test_struct_result.zig test_structs.zig test_switch.zig test_switch_continue.zig test_switch_continue_equivalent.z test_switch_dispatch_loop.zig test_switch_modify_tagged_union.z test_switch_non-exhaustive.zig test_switch_on_errors.zig test_switch_tagged_union.zig test_tagName.zig test_tagged_union.zig test_tagged_union_with_tag_values test_this_builtin.zig test_thread_local_variables.zig test_truncate_builtin.zig test_tuples.zig test_type_coercion.zig test_unhandled_enumeration_value. test_union_method.zig test_unreachable.zig test_unresolved_comptime_value.zi test_variable_alignment.zig test_variable_func_alignment.zig test_variadic_function.zig test_vector.zig test_void_ignored.zig test_void_in_hashmap.zig test_volatile.zig test_wasmMemoryGrow_builtin.zig test_while.zig test_while_break.zig test_while_continue.zig test_while_continue_expression.zi test_while_else.zig test_while_error_capture.zig test_while_nested_break.zig test_while_null_capture.zig test_without_setEvalBranchQuota_b test_wraparound_semantics.zig test_wrong_union_access.zig testing_detect_leak.zig testing_detect_test.zig testing_error_with_if.zig testing_failure.zig testing_introduction.zig testing_namespace.zig testing_null_with_if.zig testing_skip.zig tldoc_comments.zig try.zig unattached_doc-comment.zig undefined_active_union_field.zig values.zig var_must_be_initialized.zig wasi_args.zig wasi_preopens.zig zero_bit_types.zig
1 const expectEqual = @import("std").testing.expectEqual; 2 3 test "for basics" { 4 const items = [_]i32{ 4, 5, 3, 4, 0 }; 5 var sum: i32 = 0; 6 7 // For loops iterate over slices and arrays. 8 for (items) |value| { 9 // Break and continue are supported. 10 if (value == 0) { 11 continue; 12 } 13 sum += value; 14 } 15 try expectEqual(16, sum); 16 17 // To iterate over a portion of a slice, reslice. 18 for (items[0..1]) |value| { 19 sum += value; 20 } 21 try expectEqual(20, sum); 22 23 // To access the index of iteration, specify a second condition as well 24 // as a second capture value. 25 var sum2: i32 = 0; 26 for (items, 0..) |_, i| { 27 try expectEqual(usize, @TypeOf(i)); 28 sum2 += @as(i32, @intCast(i)); 29 } 30 try expectEqual(10, sum2); 31 32 // To iterate over consecutive integers, use the range syntax. 33 // Unbounded range is always a compile error. 34 var sum3: usize = 0; 35 for (0..5) |i| { 36 sum3 += i; 37 } 38 try expectEqual(10, sum3); 39 } 40 41 test "multi object for" { 42 const items = [_]usize{ 1, 2, 3 }; 43 const items2 = [_]usize{ 4, 5, 6 }; 44 var count: usize = 0; 45 46 // Iterate over multiple objects. 47 // All lengths must be equal at the start of the loop, otherwise detectable 48 // illegal behavior occurs. 49 for (items, items2) |i, j| { 50 count += i + j; 51 } 52 53 try expectEqual(21, count); 54 } 55 56 test "for reference" { 57 var items = [_]i32{ 3, 4, 2 }; 58 59 // Iterate over the slice by reference by 60 // specifying that the capture value is a pointer. 61 for (&items) |*value| { 62 value.* += 1; 63 } 64 65 try expectEqual(4, items[0]); 66 try expectEqual(5, items[1]); 67 try expectEqual(3, items[2]); 68 } 69 70 test "for else" { 71 // For allows an else attached to it, the same as a while loop. 72 const items = [_]?i32{ 3, 4, null, 5 }; 73 74 // For loops can also be used as expressions. 75 // Similar to while loops, when you break from a for loop, the else branch is not evaluated. 76 var sum: i32 = 0; 77 const result = for (items) |value| { 78 if (value != null) { 79 sum += value.?; 80 } 81 } else blk: { 82 try expectEqual(12, sum); 83 break :blk sum; 84 }; 85 try expectEqual(12, result); 86 } 87 88 // test 89