Changed options structure and added verbose option.

This commit is contained in:
Nicolás A. Ortega Froysa 2021-03-02 18:09:24 +01:00
parent efe9ec5b3b
commit 4e83d73810

View File

@ -16,17 +16,33 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
use std::env; //use std::env;
use structopt::StructOpt;
use std::path::PathBuf;
use std::collections::VecDeque; use std::collections::VecDeque;
#[derive(StructOpt)]
struct Opt {
#[structopt(short, long)]
verbose:bool,
//#[structopt(short, long)]
//import:Option<PathBuf>,
n:usize,
}
fn main() { fn main() {
let args:Vec<String> = env::args().collect(); //let args:Vec<String> = env::args().collect();
let opts = Opt::from_args();
// get the first `n` primes // get the first `n` primes
let n:usize = args[1].parse().unwrap(); //let n:usize = args[1].parse().unwrap();
let n = opts.n;
let mut primes:VecDeque<u64> = VecDeque::with_capacity(n); let mut primes:VecDeque<u64> = VecDeque::with_capacity(n);
// first prime // first prime
if opts.verbose
{
println!("{}", 2); println!("{}", 2);
}
primes.push_back(2); primes.push_back(2);
let mut candidate:u64 = 3; let mut candidate:u64 = 3;
@ -45,10 +61,18 @@ fn main() {
} }
if is_prime if is_prime
{
if opts.verbose
{ {
println!("{}", candidate); println!("{}", candidate);
}
primes.push_back(candidate); primes.push_back(candidate);
} }
candidate += 2; candidate += 2;
} }
if !opts.verbose
{
println!("{}", primes.back().unwrap());
}
} }