From d65ee86575f57ae818d067fe84535ee312135082 Mon Sep 17 00:00:00 2001 From: Marcus Stollsteimer Date: Sat, 4 Nov 2023 20:25:23 +0100 Subject: [PATCH 1/2] Improve example in "Getting the arguments" Show the complete `main()` function. --- Cargo.toml | 4 ++++ src/tutorial/cli-args-vars.rs | 6 ++++++ src/tutorial/cli-args.md | 4 ++-- 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 src/tutorial/cli-args-vars.rs diff --git a/Cargo.toml b/Cargo.toml index dd480a8..69915ab 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,6 +4,10 @@ version = "0.1.0" authors = ["Pascal Hertleif "] 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" diff --git a/src/tutorial/cli-args-vars.rs b/src/tutorial/cli-args-vars.rs new file mode 100644 index 0000000..c15132a --- /dev/null +++ b/src/tutorial/cli-args-vars.rs @@ -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"); +} diff --git a/src/tutorial/cli-args.md b/src/tutorial/cli-args.md index 4a8bb2b..c883a74 100644 --- a/src/tutorial/cli-args.md +++ b/src/tutorial/cli-args.md @@ -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 From 6c5a73e4bc0fa7253bd13a611f38d96ad01f1cb5 Mon Sep 17 00:00:00 2001 From: Marcus Stollsteimer Date: Sat, 4 Nov 2023 20:26:02 +0100 Subject: [PATCH 2/2] Improve example in "CLI arguments as data type" Show the complete `main()` function. Also, fix code indentation and a wrong line number in include. --- src/tutorial/cli-args-struct.rs | 13 ++++++------- src/tutorial/cli-args.md | 4 ++-- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/src/tutorial/cli-args-struct.rs b/src/tutorial/cli-args-struct.rs index d7f1b2d..9839558 100644 --- a/src/tutorial/cli-args-struct.rs +++ b/src/tutorial/cli-args-struct.rs @@ -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), + }; } diff --git a/src/tutorial/cli-args.md b/src/tutorial/cli-args.md index c883a74..d6d7b81 100644 --- a/src/tutorial/cli-args.md +++ b/src/tutorial/cli-args.md @@ -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`]) @@ -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.