Skip to content

Commit

Permalink
Merge pull request #231 from stomar/improve-code-examples
Browse files Browse the repository at this point in the history
Improve code examples in args parsing chapter
  • Loading branch information
pksunkara authored Nov 10, 2023
2 parents aec2cc7 + 6c5a73e commit f5c9c10
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ version = "0.1.0"
authors = ["Pascal Hertleif <[email protected]>"]
edition = "2018"

[[bin]]
name = "cli-args-vars"
path = "src/tutorial/cli-args-vars.rs"

[[bin]]
name = "cli-args-struct"
path = "src/tutorial/cli-args-struct.rs"
Expand Down
13 changes: 6 additions & 7 deletions src/tutorial/cli-args-struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ struct Cli {
}

fn main() {
let pattern = std::env::args().nth(1).expect("no pattern given");
let path = std::env::args().nth(2).expect("no path given");

let pattern = std::env::args().nth(1).expect("no pattern given");
let path = std::env::args().nth(2).expect("no path given");
let args = Cli {
pattern: pattern,
path: std::path::PathBuf::from(path),
};

let args = Cli {
pattern: pattern,
path: std::path::PathBuf::from(path),
};
}
6 changes: 6 additions & 0 deletions src/tutorial/cli-args-vars.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#![allow(unused)]

fn main() {
let pattern = std::env::args().nth(1).expect("no pattern given");
let path = std::env::args().nth(2).expect("no path given");
}
8 changes: 4 additions & 4 deletions src/tutorial/cli-args.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ the ones that follow are what the user wrote afterwards.
[`std::env::args()`]: https://doc.rust-lang.org/1.39.0/std/env/fn.args.html
[iterator]: https://doc.rust-lang.org/1.39.0/std/iter/index.html

Getting the raw arguments this way is quite easy (in file `src/main.rs`, after `fn main() {`):
Getting the raw arguments this way is quite easy (in file `src/main.rs`):

```rust,ignore
{{#include cli-args-struct.rs:10:11}}
{{#include cli-args-vars.rs:3:6}}
```

## CLI arguments as data type
Expand All @@ -65,7 +65,7 @@ way of looking at CLI arguments fits very well. Let's start with this (in file
`src/main.rs`, before `fn main() {`):

```rust,ignore
{{#include cli-args-struct.rs:3:7}}
{{#include cli-args-struct.rs:3:6}}
```

This defines a new structure (a [`struct`])
Expand All @@ -89,7 +89,7 @@ and build the structure ourselves.
It would look something like this:

```rust,ignore
{{#include cli-args-struct.rs:10:15}}
{{#include cli-args-struct.rs:8:16}}
```

This works, but it's not very convenient.
Expand Down

0 comments on commit f5c9c10

Please sign in to comment.