Reading arguments

To read arguments, ArgMatches contains a value_of method, where you add the name of a parameter. In this case, it is convenient to use constants to avoid typos. Extract the  --address argument, and if this does not exist, then check the ADDRESS environment variable. This means that the command-line argument is a higher priority than the environment variable and you can override the parameters from the .env file with command-line parameters:

let addr = matches.value_of("address")
.map(|s| s.to_owned())
.or(env::var("ADDRESS").ok())
.unwrap_or_else(|| "127.0.0.1:8080".into())
.parse()
.expect("can't parse ADDRESS variable");

In this code, we have converted all of the provided string references with the &str type to solid String objects. This is useful if you want to use the object later in the code or if you need to move it elsewhere.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.139.169