finished ch12.6 and ch12
All checks were successful
Test Gitea Actions / first (push) Successful in 22s

This commit is contained in:
darkicewolf50 2025-02-19 22:26:43 +00:00
parent 17d55c746d
commit d499a994af
3 changed files with 92 additions and 3 deletions

View File

@ -1291,3 +1291,85 @@ Another exercise try controlling case sensitivity through either a command line
Decide whether the command line arg or the env varaible shold take precedence if the program is run with one set to case sensitive and one set to ignore case Decide whether the command line arg or the env varaible shold take precedence if the program is run with one set to case sensitive and one set to ignore case
The `std::env` module contains many more useful features for dealing with env variables, see its docs to see what is available. The `std::env` module contains many more useful features for dealing with env variables, see its docs to see what is available.
## Eighth Bonus Goal: Writing Error Messages to Standard Error Instead of Standard Output
At the moment we are 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
- *standard error* (`stderr`) for error messages
This distinction enables users to choose to direct the successful outpt of a program of a file but still print error mesages to the screen
### Checking Where Errors Are Written
Lets observe how the content printed by `minigrep` is currently being written to the std output including any error messages we want to write to standard error instead
We will do that by redirecting the std output stream to a file while intentionally causing an error.
We wont redirect the std error stream, so any content sent to std error wil continue to display on the screen.
Command line programs are expected to send error messages to the std error stream so we can still continue to display on the screen even if we redirect the std output stream to a file
Our program is currently well behaved: we are about to see that it saves the error message output ot a file instead
To demonstrate this behavior we will run the program with `>` and the file path, *output.txt* that we want to redirect the std output stream to
We won't pass any args which should cause an error
```
$ cargo run > output.txt
```
The `>` syntax tells the shell to write the contents of the std output ot *output.txt* instead of the screen
We didnt see the error message we were expecting printed to the screen so that means it ended up in the file.
Here is what *output.txt* contains
```
Problem parsing arguments: not enough arguments
```
Our error message is being porinted to the std output.
It is much more useful for error messages like this to be printed to std error so only data from a successful run ends up in the file.
Let's change that
### Printing Errors to Standard Error
lets change how error messages ar printed
Becasue of the refactoring we did earlier in this chapter all the code that prints error messages is in one function `main`
The std library provides the `eprintln!` macro that prints to the std error stream lets change the two places we were calling `println!` to print errors to use `eprintln!` instead
```rust
fn main() {
let args: Vec<String> = env::args().collect();
let config = Config::build(&args).unwrap_or_else(|err| {
eprintln!("Problem parsing arguments: {err}");
process::exit(1);
});
if let Err(e) = minigrep::run(config) {
eprintln!("Application error: {e}");
process::exit(1);
}
}
```
Now if we run the program again in the same waywithout any args and redirecting the td output with `>`
```
$ cargo run > output.txt
Problem parsing arguments: not enough arguments
```
Now we see the error onscreen and *output.txt* contains nothing which is the behavior we epect line programs
Now let's run the program again with args that dont cause an error by still redirect std output ot a file, like this:
```
$ cargo run -- to poem.txt > output.txt
```
Notice that you dont get any output to the terminal
The *output.txt* will contain our results
```
Are you nobody, too?
How dreary to be somebody!
```
This now demonstrates that we now are using std output for successful output and std error for error output as appropriate

2
minigrep/output.txt Normal file
View File

@ -0,0 +1,2 @@
Are you nobody, too?
How dreary to be somebody!

View File

@ -24,8 +24,10 @@ fn main() {
// recfactor 6 // recfactor 6
let config = Config::build(&args).unwrap_or_else(|err| { let config = Config::build(&args).unwrap_or_else(|err| {
// refactor 8 // refactor 14
println!("Problem parsing arguments: {err}"); // println!("Problem parsing arguments: {err}");
// process::exit(1);
eprintln!("Problem parsing arguments: {err}");
process::exit(1); process::exit(1);
}); });
@ -36,7 +38,10 @@ fn main() {
// refactor 8 // refactor 8
if let Err(e) = minigrep::run(config) { if let Err(e) = minigrep::run(config) {
// needed for helping the user // needed for helping the user
println!("Application error: {e}"); // refactpr 14
// println!("Application error: {e}");
// process::exit(1);
eprintln!("Application error: {e}");
process::exit(1); process::exit(1);
} }