diff --git a/src/getopt.rs b/src/getopt.rs index 81c146d..70ee801 100644 --- a/src/getopt.rs +++ b/src/getopt.rs @@ -29,7 +29,7 @@ pub enum OptError { UnknownOpt, } -// Function signature +/* function signature */ pub trait GetOpt { fn getopt(&self, optstring: &str) -> Option>; } @@ -43,10 +43,11 @@ impl GetOpt for Vec { .map(Result::unwrap) .collect(); + /* these operations must be separated out into separate operations so + * the CStrings can live long enough */ let argv: Vec<_> = c_strings.iter().map(|x| x.as_ptr()).collect(); let argv_ptr = argv.as_ptr() as *const *mut c_char; - let optstring_c = CString::new(optstring).unwrap(); - let opts = optstring_c.into_raw(); + let opts = CString::new(optstring).unwrap().into_raw(); let len = self.len() as c_int; unsafe { @@ -67,10 +68,10 @@ impl GetOpt for Vec { * * Otherwise, getopt() shall return -1 when all command line * options are parsed. */ - 58 => { /* ASCII value for ':' */ + 58 => { /* numerical ASCII value for ':' */ return Some(Err(OptError::MissingArg)); }, - 63 => { /* ASCII value for '?' */ + 63 => { /* numerical ASCII value for '?' */ return Some(Err(OptError::UnknownOpt)) }, /* From getopt(3p):