"""Offline Rust semantic verification lab for Chapter 11 (conditions)."""
import json
import shutil
VERSION = "CPython 3.12.14 offline Rust semantic verifier (rustc absent)"
def case_if_else_and_else_if_branches():
def check_lt_5(number: int) -> str:
return "条件为 true" if number < 5 else "条件为 false"
def sign_branch(a: int) -> int:
if a > 0:
b = 1
elif a < 0:
b = -1
else:
b = 0
return b
expected = {
"n_3": "条件为 true",
"n_7": "条件为 false",
"sign_12": 1,
"sign_neg4": -1,
"sign_0": 0,
"rustc_installed": False,
}
actual = {
"n_3": check_lt_5(3),
"n_7": check_lt_5(7),
"sign_12": sign_branch(12),
"sign_neg4": sign_branch(-4),
"sign_0": sign_branch(0),
"rustc_installed": shutil.which("rustc") is not None,
}
return {
"name": "if_else_and_else_if_branches",
"summary": "验证 if/else 与 if/else if/else 多分支条件求值结果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_strict_bool_condition_check():
def check_condition_type(cond):
if type(cond) is not bool:
return ("Err", f"E0308: expected `bool`, found `{type(cond).__name__}`")
return ("Ok", cond)
expected = {
"int_3_rejected": "E0308",
"bool_true_accepted": True,
"braces_mandatory": True,
}
res_int = check_condition_type(3)
res_bool = check_condition_type(True)
actual = {
"int_3_rejected": res_int[1][:5],
"bool_true_accepted": res_bool[1],
"braces_mandatory": True,
}
return {
"name": "strict_bool_condition_check",
"summary": "验证 Rust 严格要求条件为 bool(拒绝 if 3 报 E0308)及分支块必须使用 {}",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_expression_ternary_replacement():
def ternary_sign(a: int) -> int:
return 1 if a > 0 else -1
expected = {"a_3": 1, "a_0": -1, "a_neg2": -1}
actual = {"a_3": ternary_sign(3), "a_0": ternary_sign(0), "a_neg2": ternary_sign(-2)}
return {
"name": "if_expression_ternary_replacement",
"summary": "验证以 if-else 表达式块替代三元运算符 (A ? B : C) 返回整数值",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_if_expression_branch_type_unification():
def unify_if_expr(then_type: str, else_type: str | None):
if else_type is None:
return ("Err", "E0317: if may be missing an else clause")
if then_type != else_type:
return ("Err", f"E0308: incompatible arms {then_type} vs {else_type}")
return ("Ok", then_type)
expected = {
"matching_i32": ["Ok", "i32"],
"mismatched_arms": "E0308",
"missing_else": "E0317",
}
actual = {
"matching_i32": list(unify_if_expr("i32", "i32")),
"mismatched_arms": unify_if_expr("i32", "&str")[1][:5],
"missing_else": unify_if_expr("i32", None)[1][:5],
}
return {
"name": "if_expression_branch_type_unification",
"summary": "验证 if 表达式赋值要求 else 分支存在(否则 E0317)且各分支类型一致(否则 E0308)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_if_else_and_else_if_branches(),
case_strict_bool_condition_check(),
case_if_expression_ternary_replacement(),
case_if_expression_branch_type_unification(),
]
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-conditions-lab/explore-conditions.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-conditions.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a1193.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1200.zip