知识卡片
分离标准输出和错误
用 println! 将匹配行写往 stdout,用 eprintln! 将诊断写往 stderr。调用者可通过 > output.txt 捕获正常数据。故意少传参数并重定向输出,可检查错误是否仍可见,结果文件是否保持为空。
这张卡片看不懂?
这张卡片已通过人工审查,不再接受失败反馈。
已有反馈 · 0 条
还没有人提交反馈。
在整书笔记中的位置
原书出处 · 1 处
- I/O 项目:构建命令行程序 Writing Error Messages to Standard Error Instead of Standard Output · 查看原书依据
At the moment, we’re writing all of our output to the terminal using the println! macro. In most terminals, there are two kinds of output: standard output (stdout) for general information and standard error (stderr) for error messages. This distinction enables users to choose to direct the successful output of a program to a file but still print error messages to the screen.
参考来源
- 位置:《The Rust Programming Language(第2版)》第12章 I/O 项目:构建命令行程序(来源项目 a19c7933-4eda-4176-aebd-30fe7e1ad292,`extracted/The Rust Programming Language(第2版)/_epub-src/c12.xhtml`,块 0293)
- 结论依据:原文用两个流的不同用途解释重定向仍能保留错误诊断。
- 原始内容:At the moment, we’re writing all of our output to the terminal using the println! macro. In most terminals, there are two kinds of output: standard output (stdout) for general information and standard error (stderr) for error messages. This distinction enables users to choose to direct the successful output of a program to a file but still print error messages to the screen.