蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线校验 Rust while、for 迭代器/范围遍历与 loop break 表达式返回值

PYTHONWindows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。explore-loop.py
explore-loop.py126 行
"""Offline Rust semantic verification lab for Chapter 12 (loop)."""
import json
import shutil

VERSION = "CPython 3.12.14 offline Rust semantic verifier (rustc absent)"


def case_while_loop_and_c_for_equivalent():
    number = 1
    out = []
    while number != 4:
        out.append(str(number))
        number += 1
    out.append("EXIT")

    # C-style for (i = 0; i < 10; i++) rewritten with Rust while
    i = 0
    total = 0
    while i < 10:
        total += i
        i += 1

    expected = {
        "while_output": ["1", "2", "3", "EXIT"],
        "while_0_to_9_sum": 45,
        "while_final_i": 10,
        "do_is_reserved_not_dowhile": True,
        "rustc_installed": False,
    }
    actual = {
        "while_output": out,
        "while_0_to_9_sum": total,
        "while_final_i": i,
        "do_is_reserved_not_dowhile": True,
        "rustc_installed": shutil.which("rustc") is not None,
    }
    return {
        "name": "while_loop_and_c_for_equivalent",
        "summary": "验证 while 条件循环输出 1/2/3/EXIT、替代 C 三段式 for 的计数循环及无 do-while 规则",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_for_iter_array_traversal():
    a = [10, 20, 30, 40, 50]
    lines = [f"值为 : {val}" for val in iter(a)]
    expected = ["值为 : 10", "值为 : 20", "值为 : 30", "值为 : 40", "值为 : 50"]
    return {
        "name": "for_iter_array_traversal",
        "summary": "验证 for i in a.iter() 遍历定长数组 [10, 20, 30, 40, 50] 的全部输出",
        "expected": expected,
        "actual": lines,
        "passed": lines == expected,
    }


def case_for_range_index_traversal():
    a = [10, 20, 30, 40, 50]
    lines = [f"a[{idx}] = {a[idx]}" for idx in range(0, 5)]
    expected = ["a[0] = 10", "a[1] = 20", "a[2] = 30", "a[3] = 40", "a[4] = 50"]
    return {
        "name": "for_range_index_traversal",
        "summary": "验证 for i in 0..5 按半开范围下标访问数组元素",
        "expected": expected,
        "actual": lines,
        "passed": lines == expected,
    }


def case_loop_mid_exit_break():
    chars = ["R", "U", "S", "T", "L", "A", "B"]
    i = 0
    emitted = []
    while True:
        ch = chars[i]
        if ch == "T":
            break
        emitted.append(f"'{ch}'")
        i += 1
    expected = ["'R'", "'U'", "'S'"]
    return {
        "name": "loop_mid_exit_break",
        "summary": "模拟原生无限循环 loop 在循环体中部检测目标字符并执行 break 退出",
        "expected": expected,
        "actual": emitted,
        "passed": emitted == expected,
    }


def case_loop_expression_break_value():
    chars = ["R", "U", "S", "T", "L", "A", "B"]
    i = 0
    location = None
    while True:
        ch = chars[i]
        if ch == "T":
            location = i
            break
        i += 1
    expected = {"found_char": "T", "location": 3, "formatted": "'T' 的索引为 3"}
    actual = {"found_char": chars[location], "location": location, "formatted": f"'T' 的索引为 {location}"}
    return {
        "name": "loop_expression_break_value",
        "summary": "验证 loop 作为表达式配合 break i 向外返回查找到的索引值 3",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_while_loop_and_c_for_equivalent(),
        case_for_iter_array_traversal(),
        case_for_range_index_traversal(),
        case_loop_mid_exit_break(),
        case_loop_expression_break_value(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。

入口文件:projects/rust-loop-lab/explore-loop.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-loop.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Rust

课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a1193.zip

下载课程关联工程包:Rust

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1210.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习