【发布时间】:2019-09-01 20:00:12
【问题描述】:
use std::env;
use std::process;
fn main(){
let mut repository_urls: Vec<String> = env::args().collect();
if repository_urls.len() == 1 {
eprintln!("You have to enter at least one repository url!");
process::exit(1);
} else {
for i in 0 .. repository_urls.len() - 1 {
if repository_urls[i + 1] == "--advanced" && repository_urls.len() >= i + 5 {
repository_urls.remove(i + 4);
repository_urls.remove(i + 3);
repository_urls.remove(i + 2);
} else {
}
}
//Ends the process normally
process::exit(0);
}
}
如果你有 --advanced 1 2 3 作为这个参数,它会做所有事情并按预期执行所有逻辑,但是当它完成时它最终会崩溃:
thread 'main' panicked at 'index out of bounds: the len is 2 but the index is 2', /rustc/a53f9df32fbb0b5f4382caaad8f1a46f36ea887c/src/libcore/slice/mod.rs:2695:10
note: Run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
如何让我的程序正常退出而不会崩溃?
【问题讨论】:
标签: rust