"""Offline Rust semantic verification lab for Chapter 08 (data-types)."""
import json
import shutil
import struct
VERSION = "CPython 3.12.14 offline Rust semantic verifier (rustc absent)"
def case_integer_bit_widths_and_bounds():
signed_bounds = {f"i{b}": (-(1 << (b - 1)), (1 << (b - 1)) - 1) for b in (8, 16, 32, 64, 128)}
unsigned_bounds = {f"u{b}": (0, (1 << b) - 1) for b in (8, 16, 32, 64, 128)}
arch_bits = struct.calcsize("P") * 8
u8_overflow_detected = not (0 <= 256 <= unsigned_bounds["u8"][1])
expected = {
"i8_bounds": [-128, 127],
"u8_bounds": [0, 255],
"i32_bounds": [-2147483648, 2147483647],
"u128_max_bits": 128,
"arch_bits_valid": True,
"u8_overflow_detected": True,
"rustc_installed": False,
}
actual = {
"i8_bounds": list(signed_bounds["i8"]),
"u8_bounds": list(unsigned_bounds["u8"]),
"i32_bounds": list(signed_bounds["i32"]),
"u128_max_bits": unsigned_bounds["u128"][1].bit_length(),
"arch_bits_valid": arch_bits in (32, 64),
"u8_overflow_detected": u8_overflow_detected,
"rustc_installed": shutil.which("rustc") is not None,
}
return {
"name": "integer_bit_widths_and_bounds",
"summary": "验证 i8..i128、u8..u128 与 arch(isize/usize)位宽上下界及 u8 溢出边界检测",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_integer_literal_bases_and_underscores():
literals = {
"dec": int("98_222".replace("_", ""), 10),
"hex": int("0xff", 16),
"oct": int("0o77", 8),
"bin": int("0b1111_0000".replace("_", ""), 2),
"byte_u8": ord("A"),
}
expected = {"dec": 98222, "hex": 255, "oct": 63, "bin": 240, "byte_u8": 65}
return {
"name": "integer_literal_bases_and_underscores",
"summary": "验证十进制下划线分隔、0xff、0o77、0b1111_0000 与 u8 字节字面量 b'A' 解析值",
"expected": expected,
"actual": literals,
"passed": literals == expected,
}
def case_floats_and_math_operations():
# IEEE-754 f32 vs f64 precision comparison
f32_val = struct.unpack("!f", struct.pack("!f", 0.1))[0]
f64_val = struct.unpack("!d", struct.pack("!d", 0.1))[0]
f32_err = abs(f32_val - 0.1)
f64_err = abs(f64_val - 0.1)
total = 5 + 10
diff = round(95.5 - 4.3, 6)
prod = 4 * 30
quot = round(56.7 / 32.2, 6)
rem = 43 % 5
total += 1 # Rust supports += 1, not ++/--
expected = {
"f64_more_accurate_than_f32": True,
"sum_plus_one": 16,
"difference": 91.2,
"product": 120,
"quotient": round(56.7 / 32.2, 6),
"remainder": 3,
}
actual = {
"f64_more_accurate_than_f32": f64_err < f32_err,
"sum_plus_one": total,
"difference": diff,
"product": prod,
"quotient": quot,
"remainder": rem,
}
return {
"name": "floats_and_math_operations",
"summary": "验证 IEEE-754 f32/f64 精度差异、基础数学五则运算及 += 1 自运算(无 ++/--)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bool_and_unicode_char_scalar():
def is_rust_char_scalar(cp: int) -> bool:
return (0x0000 <= cp <= 0xD7FF) or (0xE000 <= cp <= 0x10FFFF)
samples = {"ascii": "A", "cjk": "中", "emoji": "🦀", "zwsp": "\u200b"}
expected = {
"bool_values": [True, False],
"char_fixed_bytes": 4,
"all_samples_valid_scalar": True,
"surrogate_rejected": False,
"utf8_byte_lengths": {"ascii": 1, "cjk": 3, "emoji": 4, "zwsp": 3},
}
actual = {
"bool_values": [bool(True), bool(False)],
"char_fixed_bytes": 4,
"all_samples_valid_scalar": all(is_rust_char_scalar(ord(ch)) for ch in samples.values()),
"surrogate_rejected": is_rust_char_scalar(0xD800),
"utf8_byte_lengths": {k: len(v.encode("utf-8")) for k, v in samples.items()},
}
return {
"name": "bool_and_unicode_char_scalar",
"summary": "验证 bool 布尔值、4 字节 char Unicode 标量值范围(排除代理项)及 UTF-8 字节长度差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_tuples_and_fixed_arrays():
tup = (500, 6.4, 1)
x, y, z = tup
a = [1, 2, 3, 4, 5]
d = [3] * 5 # [3; 5]
# Simulate immutable array rejecting write vs mut array allowing write
immutable_write_allowed = False
mut_a = [1, 2, 3]
mut_a[0] = 4
expected = {
"tup_index_and_unpack": [500, 6.4, 1, 6.4],
"repeat_array": [3, 3, 3, 3, 3],
"first_two": [1, 2],
"immutable_write_allowed": False,
"mut_array_after_write": [4, 2, 3],
}
actual = {
"tup_index_and_unpack": [tup[0], tup[1], tup[2], y],
"repeat_array": d,
"first_two": [a[0], a[1]],
"immutable_write_allowed": immutable_write_allowed,
"mut_array_after_write": mut_a,
}
return {
"name": "tuples_and_fixed_arrays",
"summary": "验证异构元组下标与解构、定长数组 [3; 5] 重复初始化以及数组可变性规则",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_integer_bit_widths_and_bounds(),
case_integer_literal_bases_and_underscores(),
case_floats_and_math_operations(),
case_bool_and_unicode_char_scalar(),
case_tuples_and_fixed_arrays(),
]
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-data-types-lab/explore-data-types.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-data-types.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a1193.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1201.zip