mirror of
https://github.com/rust-lang-cn/book-cn.git
synced 2025-02-02 23:38:41 +08:00
Fix code examples and output
This commit is contained in:
parent
1a7c4764e7
commit
901aa3d788
@ -562,7 +562,7 @@ error message. /Carol -->
|
||||
|
||||
When we compile this, we get the following error message:
|
||||
|
||||
```bash
|
||||
```text
|
||||
error[E0308]: mismatched types
|
||||
-->
|
||||
|
|
||||
|
@ -191,7 +191,7 @@ vector:
|
||||
|
||||
<span class="filename">Filename: src/lib.rs</span>
|
||||
|
||||
```rust,ignore
|
||||
```rust
|
||||
fn grep<'a>(search: &str, contents: &'a str) -> Vec<&'a str> {
|
||||
let mut results = Vec::new();
|
||||
|
||||
|
@ -19,7 +19,7 @@ The `>` syntax tells the shell to write the contents of standard out to
|
||||
running we'll see our error message:
|
||||
|
||||
```text
|
||||
Application error: No search string or filename found
|
||||
Problem parsing arguments: not enough arguments
|
||||
```
|
||||
|
||||
We'd like this to be printed to the screen instead, and only have the output
|
||||
@ -34,19 +34,25 @@ extern crate greprs;
|
||||
|
||||
use std::env;
|
||||
use std::process;
|
||||
use std::io::prelude::*;
|
||||
|
||||
use greprs::Config;
|
||||
|
||||
fn main() {
|
||||
let mut stderr = std::io::stderr();
|
||||
let args: Vec<String> = env::args().collect();
|
||||
|
||||
let config = Config::new(&args).unwrap_or_else(|err| {
|
||||
println!("Problem parsing arguments: {}", err);
|
||||
writeln!(
|
||||
&mut stderr,
|
||||
"Problem parsing arguments: {}",
|
||||
err
|
||||
).expect("Could not write to stderr");
|
||||
|
||||
process::exit(1);
|
||||
});
|
||||
|
||||
if let Err(e) = greprs::run(config) {
|
||||
let mut stderr = std::io::stderr();
|
||||
|
||||
writeln!(
|
||||
&mut stderr,
|
||||
@ -82,7 +88,7 @@ redirecting `stdout` with `>`:
|
||||
|
||||
```text
|
||||
$ cargo run > output.txt
|
||||
Application error: No search string or filename found
|
||||
Problem parsing arguments: not enough arguments
|
||||
```
|
||||
|
||||
Now we see our error on the screen, but `output.txt` contains nothing. If we
|
||||
|
Loading…
Reference in New Issue
Block a user