知识卡片
读取整个文本文件
std::fs::read_to_string(path) 把整个文本读为 String。文件不存在、权限不足或文本编码不满足要求时,得到 Err。原型用 expect 快速暴露失败,有需要继续向调用方报告时可保留 Result。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- I/O 项目:构建命令行程序 Reading a File · 查看原书依据
In main, the new statement fs::read_to_string takes the file_path, opens that file, and returns an std::io::Result<String> of the file’s contents ❷.
参考来源
- 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0052)
- 结论依据:原文指出 `fs::read_to_string` 的返回类型及其来源。
- 原始内容:In main, the new statement fs::read_to_string takes the file_path, opens that file, and returns an std::io::Result<String> of the file’s contents ❷.