]> git.proxmox.com Git - rustc.git/blobdiff - src/doc/book/src/ch12-01-accepting-command-line-arguments.md
New upstream version 1.63.0+dfsg1
[rustc.git] / src / doc / book / src / ch12-01-accepting-command-line-arguments.md
index 4228bf5d30a4e98889e28093c757d1ca423e126e..bb69366633f03101111bc38cec4c6a00f4034ae2 100644 (file)
@@ -11,12 +11,13 @@ $ cd minigrep
 ```
 
 The first task is to make `minigrep` accept its two command line arguments: the
-filename and a string to search for. That is, we want to be able to run our
-program with `cargo run`, a string to search for, and a path to a file to
-search in, like so:
+file path and a string to search for. That is, we want to be able to run our
+program with `cargo run`, two hyphens to indicate the following arguments are
+for our program rather than for `cargo`, a string to search for, and a path to
+a file to search in, like so:
 
 ```console
-$ cargo run searchstring example-filename.txt
+$ cargo run -- searchstring example-filename.txt
 ```
 
 Right now, the program generated by `cargo new` cannot process arguments we
@@ -51,11 +52,11 @@ First, we bring the `std::env` module into scope with a `use` statement so we
 can use its `args` function. Notice that the `std::env::args` function is
 nested in two levels of modules. As we discussed in [Chapter
 7][ch7-idiomatic-use]<!-- ignore -->, in cases where the desired function is
-nested in more than one module, it’s conventional to bring the parent module
-into scope rather than the function. By doing so, we can easily use other
-functions from `std::env`. It’s also less ambiguous than adding `use
-std::env::args` and then calling the function with just `args`, because `args`
-might easily be mistaken for a function that’s defined in the current module.
+nested in more than one module, we’ve chosen to bring the parent module into
+scope rather than the function. By doing so, we can easily use other functions
+from `std::env`. It’s also less ambiguous than adding `use std::env::args` and
+then calling the function with just `args`, because `args` might easily be
+mistaken for a function that’s defined in the current module.
 
 > ### The `args` Function and Invalid Unicode
 >
@@ -74,8 +75,8 @@ want a vector of strings. Although we very rarely need to annotate types in
 Rust, `collect` is one function you do often need to annotate because Rust
 isn’t able to infer the kind of collection you want.
 
-Finally, we print the vector using the debug formatter, `:?`. Let’s try running
-the code first with no arguments and then with two arguments:
+Finally, we print the vector using the debug macro. Let’s try running the code
+first with no arguments and then with two arguments:
 
 ```console
 {{#include ../listings/ch12-an-io-project/listing-12-01/output.txt}}
@@ -107,14 +108,14 @@ we can use the values throughout the rest of the program. We do that in Listing
 ```
 
 <span class="caption">Listing 12-2: Creating variables to hold the query
-argument and filename argument</span>
+argument and file path argument</span>
 
 As we saw when we printed the vector, the program’s name takes up the first
 value in the vector at `args[0]`, so we’re starting arguments at index `1`. The
 first argument `minigrep` takes is the string we’re searching for, so we put a
 reference to the first argument in the variable `query`. The second argument
-will be the filename, so we put a reference to the second argument in the
-variable `filename`.
+will be the file path, so we put a reference to the second argument in the
+variable `file_path`.
 
 We temporarily print the values of these variables to prove that the code is
 working as we intend. Let’s run this program again with the arguments `test`