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

离线校验 Rust 首程序源码结构、所有权规则预演与工具链探测

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

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


def case_toolchain_absence_probe():
    rustc = shutil.which("rustc")
    cargo = shutil.which("cargo")
    expected = {"rustc_found": False, "cargo_found": False, "offline_mode": True}
    actual = {
        "rustc_found": rustc is not None,
        "cargo_found": cargo is not None,
        "offline_mode": rustc is None and cargo is None,
    }
    return {
        "name": "toolchain_absence_probe",
        "summary": "如实探测本地 rustc 与 cargo 可执行文件,确认当前为离线语义验证模式",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_hello_world_source_and_format():
    filename = "hello.rs"
    src = 'fn main() {\n    println!("Hello World!");\n}\n'
    has_rs_ext = filename.endswith(".rs")
    has_main = re.search(r"\bfn\s+main\s*\(\s*\)", src) is not None
    macro_match = re.search(r'println!\("([^"]*)"\);', src)
    stdout = (macro_match.group(1) + "\n") if macro_match else ""
    expected = {
        "extension": ".rs",
        "has_main_entry": True,
        "uses_macro_bang": True,
        "stdout": "Hello World!\n",
    }
    actual = {
        "extension": ".rs" if has_rs_ext else "",
        "has_main_entry": has_main,
        "uses_macro_bang": "println!" in src,
        "stdout": stdout,
    }
    return {
        "name": "hello_world_source_and_format",
        "summary": "解析首个 .rs 源码的 main 入口、println! 宏调用与预期标准输出",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_ownership_safety_rule_preview():
    # Minimal preview of single-owner move vs Copy semantics
    owners = {"s1": "heap_buf_0", "x": "stack_i32_42"}
    # Move heap_buf_0 from s1 to s2
    owners["s2"] = owners.pop("s1")
    use_s1_after_move_blocked = "s1" not in owners
    # Copy stack value x to y
    owners["y"] = owners["x"]
    both_copy_valid = ("x" in owners) and ("y" in owners)
    expected = {
        "use_after_move_blocked": True,
        "new_owner_holds_buf": "heap_buf_0",
        "copy_keeps_both_valid": True,
    }
    actual = {
        "use_after_move_blocked": use_s1_after_move_blocked,
        "new_owner_holds_buf": owners["s2"],
        "copy_keeps_both_valid": both_copy_valid,
    }
    return {
        "name": "ownership_safety_rule_preview",
        "summary": "预演所有权唯一归属转移与栈上基础类型复制规则,说明无 GC 内存安全原理",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_official_ecosystem_domains():
    characteristics = [
        "内存安全", "并发编程", "性能", "类型系统", "错误处理", "宏系统",
        "包管理", "跨平台", "社区支持", "工具链", "无段错误", "迭代器和闭包",
    ]
    applications = [
        "系统编程", "网络编程", "游戏开发", "WebAssembly", "工具开发",
        "区块链技术", "科学计算", "音视频处理", "云计算", "嵌入式设备",
    ]
    official_urls = [
        "https://www.rust-lang.org/zh-CN",
        "https://doc.rust-lang.org/",
        "https://play.rust-lang.org/",
        "https://code.visualstudio.com/",
    ]
    expected = {
        "characteristic_count": 12,
        "application_count": 10,
        "all_https_official": True,
    }
    actual = {
        "characteristic_count": len(characteristics),
        "application_count": len(applications),
        "all_https_official": all(u.startswith("https://") for u in official_urls),
    }
    return {
        "name": "official_ecosystem_domains",
        "summary": "核对 Rust 12 项核心语言特点、10 类应用领域与官方参考入口完整性",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_toolchain_absence_probe(),
        case_hello_world_source_and_format(),
        case_ownership_safety_rule_preview(),
        case_official_ecosystem_domains(),
    ]
    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-tutorial-lab/explore-tutorial.py

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

下载课程关联工程包:Rust

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

下载课程关联工程包:Rust

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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