Rust by Example — страница 61 из 66

`rmdir a/c/d`

And the final state of the a directory is:

$ tree a

a

|-- b.txt

`-- c

`-- b.txt -> ../b.txt


1 directory, 2 files

An alternative way to define the function cat is with ? notation:

fn cat(path: &Path) -> io::Result {

let mut f = File::open(path)?;

let mut s = String::new();

f.read_to_string(&mut s)?;

Ok(s)

}

See also:

cfg!

Program arguments

Standard Library

The command line arguments can be accessed using std::env::args, which returns an iterator that yields a String for each argument:

use std::env;

fn main() {

let args: Vec = env::args().collect();

// The first argument is the path that was used to call the program.

println!("My path is {}.", args[0]);

// The rest of the arguments are the passed command line parameters.

// Call the program like this:

//   $ ./args arg1 arg2

println!("I got {:?} arguments: {:?}.", args.len() - 1, &args[1..]);

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

$ ./args 1 2 3

My path is ./args.

I got 3 arguments: ["1", "2", "3"].

Crates

Alternatively, there are numerous crates that can provide extra functionality when creating command-line applications. The Rust Cookbook exhibits best practices on how to use one of the more popular command line argument crates, clap.

Argument parsing

Matching can be used to parse simple arguments:

use std::env;

fn increase(number: i32) {

println!("{}", number + 1);

}

fn decrease(number: i32) {

println!("{}", number - 1);

}

fn help() {

println!("usage:

match_args 

Check whether given string is the answer.

match_args {{increase|decrease}} 

Increase or decrease given integer by one.");

}

fn main() {

let args: Vec = env::args().collect();

match args.len() {

// no arguments passed

1 => {

println!("My name is 'match_args'. Try passing some arguments!");

},

// one argument passed

2 => {

match args[1].parse() {

Ok(42) => println!("This is the answer!"),

_ => println!("This is not the answer."),

}

},

// one command and one argument passed

3 => {

let cmd = &args[1];

let num = &args[2];

// parse the number

let number: i32 = match num.parse() {

Ok(n) => {

n

},

Err(_) => {

eprintln!("error: second argument not an integer");

help();

return;

},

};

// parse the command

match &cmd[..] {

"increase" => increase(number),

"decrease" => decrease(number),

_ => {

eprintln!("error: invalid command");

help();

},

}

},

// all the other cases

_ => {

// show a help message

help();

}

}

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

$ ./match_args Rust

This is not the answer.

$ ./match_args 42

This is the answer!

$ ./match_args do something

error: second argument not an integer

usage:

match_args 

Check whether given string is the answer.

match_args {increase|decrease} 

Increase or decrease given integer by one.

$ ./match_args do 42

error: invalid command

usage:

match_args 

Check whether given string is the answer.

match_args {increase|decrease} 

Increase or decrease given integer by one.

$ ./match_args increase 42

43

Foreign Function Interface

Rust provides a Foreign Function Interface (FFI) to C libraries. Foreign functions must be declared inside an extern block annotated with a #[link] attribute containing the name of the foreign library.

use std::fmt;


// this extern block links to the libm library

#[link(name = "m")]

extern {

// this is a foreign function

// that computes the square root of a single precision complex number

fn csqrtf(z: Complex) -