"""Offline Rust semantic verifier for Chapter 23: File & IO (args, stdin, fs read/write, streaming, OpenOptions)."""
import io
import json
from pathlib import Path
import tempfile
def run_cases() -> list[dict]:
cases = []
# Case 1: std::env::args() iteration and std::io::stdin().read_line() buffer semantics
sim_args = ["target/debug/greeting.exe", "first", "second"]
stdin_stream = io.StringIO("HELLO_RUST\n42\n")
line_buf = stdin_stream.readline()
num_line = stdin_stream.readline()
parsed_num = int(num_line.strip())
actual_1 = {
"arg_count": len(sim_args),
"user_args": sim_args[1:],
"read_line_preserves_newline": line_buf == "HELLO_RUST\n",
"parsed_int_from_line": parsed_num,
}
expected_1 = {
"arg_count": 3,
"user_args": ["first", "second"],
"read_line_preserves_newline": True,
"parsed_int_from_line": 42,
}
cases.append(dict(
name="cli_args_and_stdin_read_line",
summary="Verify env::args() indexing/iteration and stdin().read_line() newline retention + parse",
expected=expected_1,
actual=actual_1,
passed=actual_1 == expected_1,
))
with tempfile.TemporaryDirectory() as tmp:
text_path = Path(tmp) / "text.txt"
text_path.write_text("This is a text file.", encoding="utf-8")
# Case 2: fs::read_to_string, fs::read (Vec<u8>), and 5-byte buffered File::read stream
full_str = text_path.read_text(encoding="utf-8")
full_bytes = list(text_path.read_bytes())
with text_path.open("rb") as fh:
chunk1 = list(fh.read(5))
chunk2 = list(fh.read(5))
actual_2 = {
"read_to_string": full_str,
"byte_len": len(full_bytes),
"first_5_bytes": chunk1,
"second_5_bytes": chunk2,
}
expected_2 = {
"read_to_string": "This is a text file.",
"byte_len": 20,
"first_5_bytes": [84, 104, 105, 115, 32],
"second_5_bytes": [105, 115, 32, 97, 32],
}
cases.append(dict(
name="fs_read_to_string_bytes_and_stream",
summary="Verify real temp-file text read, u8 byte vector [84,104,...], and two 5-byte streaming reads",
expected=expected_2,
actual=actual_2,
passed=actual_2 == expected_2,
))
# Case 3: fs::write / File::create truncate, OpenOptions::append, and OpenOptions read+write in-place overwrite
text_path.write_bytes(b"FROM RUST PROGRAM")
after_write = text_path.read_text(encoding="utf-8")
# OpenOptions::new().append(true)
with text_path.open("ab") as fh:
fh.write(b" APPEND WORD")
after_append = text_path.read_text(encoding="utf-8")
# OpenOptions::new().read(true).write(true) -> cursor starts at 0, overwrites prefix without truncating
with text_path.open("r+b") as fh:
fh.write(b"COVER")
after_cover = text_path.read_text(encoding="utf-8")
actual_3 = {
"after_create_write": after_write,
"after_append": after_append,
"after_rw_cover": after_cover,
}
expected_3 = {
"after_create_write": "FROM RUST PROGRAM",
"after_append": "FROM RUST PROGRAM APPEND WORD",
"after_rw_cover": "COVERRUST PROGRAM APPEND WORD",
}
cases.append(dict(
name="fs_write_append_and_open_options_rw",
summary="Verify truncate write, append mode, and read+write offset-0 overwrite (COVERRUST PROGRAM APPEND WORD)",
expected=expected_3,
actual=actual_3,
passed=actual_3 == expected_3,
))
return cases
def main() -> None:
payload = {
"version": "CPython 3.12.14 offline Rust semantic verifier (rustc absent)",
"cases": run_cases(),
}
print(json.dumps(payload, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。
入口文件:projects/rust-file-io-lab/explore-file-io.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-file-io.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14、Playwright 与本地浏览器/标准库。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联29章) · 含当前章节或实例 · mbskycom202612a1193.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1204.zip