This article provides some code samples in Rust.
Command line interface
Command arguments
cargo run foo bar baz "This string is a sentence"
1 | use std::env;
|
2 |
|
3 | // cargo run hello world
|
4 |
|
5 | fn main() {
|
6 | let args: Vec<String> = env::args().collect();
|
7 |
|
8 | // Retrieve first argument
|
9 | let input = args[1].clone();
|
10 |
|
11 | // Print all arguments
|
12 | println!("{:?}", args); // ["target/debug/executable", "foo", "bar", "baz", "This string is a sentence"]
|
13 | }
|
1 | use std::io;
|
2 |
|
3 | fn main() {
|
4 | // empty string
|
5 | let mut input = String::new();
|
6 |
|
7 | // Collecting user input
|
8 | println!("Enter your name: ");
|
9 | io::stdin()
|
10 | .read_line(&mut input)
|
11 | .expect("Failed to read line");
|
12 |
|
13 | println!("Received name: {}", input);
|
14 | }
|
15 |
|