"""Offline Rust semantic verification lab for Chapter 03 (setup)."""
import json
import shutil
import tomllib
VERSION = "CPython 3.12.14 offline Rust semantic verifier (rustc absent)"
def case_local_toolchain_probe():
expected = {"rustc": False, "cargo": False, "rustup": False}
actual = {
"rustc": shutil.which("rustc") is not None,
"cargo": shutil.which("cargo") is not None,
"rustup": shutil.which("rustup") is not None,
}
return {
"name": "local_toolchain_probe",
"summary": "探测当前离线环境 rustc、cargo 与 rustup 可用状态并如实记录缺失",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_platform_target_triple_matrix():
def resolve_host_triple(os_name: str, c_toolchain: str) -> str:
if os_name == "windows":
return "x86_64-pc-windows-msvc" if c_toolchain == "msvc" else "x86_64-pc-windows-gnu"
if os_name == "linux":
return "x86_64-unknown-linux-gnu"
if os_name == "macos":
return "aarch64-apple-darwin"
raise ValueError(os_name)
default_win = resolve_host_triple("windows", "msvc")
mingw_win = default_win.replace("msvc", "gnu")
expected = {
"windows_msvc": "x86_64-pc-windows-msvc",
"windows_mingw": "x86_64-pc-windows-gnu",
"linux_gcc": "x86_64-unknown-linux-gnu",
"macos_xcode": "aarch64-apple-darwin",
}
actual = {
"windows_msvc": default_win,
"windows_mingw": mingw_win,
"linux_gcc": resolve_host_triple("linux", "gcc"),
"macos_xcode": resolve_host_triple("macos", "xcode"),
}
return {
"name": "platform_target_triple_matrix",
"summary": "核对 Windows MSVC/MinGW GNU 宿主三元组切换与 Linux/macOS C 链接器前置依赖",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rustup_and_vscode_extension_plan():
unix_cmd = "curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh"
win_installer = "rustup-init.exe"
version_cmd = "rustc -V"
extensions = ["rust-lang.rust-analyzer", "webfreak.debug", "MS-CEINTL.vscode-language-pack-zh-hans"]
expected = {
"tls_min_1_2": True,
"win_exe": True,
"uppercase_v_flag": True,
"has_rust_analyzer": True,
}
actual = {
"tls_min_1_2": "--tlsv1.2" in unix_cmd and "https://sh.rustup.rs" in unix_cmd,
"win_exe": win_installer.endswith(".exe"),
"uppercase_v_flag": version_cmd.split()[1] == "-V",
"has_rust_analyzer": any("rust-analyzer" in e for e in extensions),
}
return {
"name": "rustup_and_vscode_extension_plan",
"summary": "验证 rustup 跨平台安装指令、大写 -V 版本检查参数与 VSCode 核心扩展清单",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_cargo_greeting_workspace_simulation():
cargo_toml = '[package]\nname = "greeting"\nversion = "0.1.0"\nedition = "2021"\n\n[dependencies]\n'
parsed = tomllib.loads(cargo_toml)
workflow = ["cargo new greeting", "cd ./greeting", "cargo build", "cargo run"]
expected = {
"package_name": "greeting",
"steps": 4,
"entry_file": "greeting/src/main.rs",
"default_output": "Hello, world!",
}
actual = {
"package_name": parsed["package"]["name"],
"steps": len(workflow),
"entry_file": f"{parsed['package']['name']}/src/main.rs",
"default_output": "Hello, world!",
}
return {
"name": "cargo_greeting_workspace_simulation",
"summary": "模拟 cargo new greeting 工程骨架、Cargo.toml 解析与构建运行命令序列",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_local_toolchain_probe(),
case_platform_target_triple_matrix(),
case_rustup_and_vscode_extension_plan(),
case_cargo_greeting_workspace_simulation(),
]
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-setup-lab/explore-setup.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-setup.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Rust 语义验证器(本地未安装 rustc/cargo)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a1193.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1217.zip