Skip to main content

FnSubSwitch

Type Alias FnSubSwitch 

Source
pub type FnSubSwitch<'a> = &'a dyn Fn(&str);
Expand description

Type for a closure for a switch.

Using RefCell to change the captured environment is required. While it isn’t strictly required, it is required if we want to reuse the closure.

(technically, this is a function pointer, not a closure, but the syntax creates the function pointer)

Example:

use std::cell::RefCell;
use getoptions_long::*;

let args: Vec<String> = vec!["-t", "2", "-t", "20", "-t", "9"].into_iter().map(|x| x.to_string()).collect();
let flag = RefCell::new(String::new());

let result = get_options(&mut [
    Opt::SubArg("t|toggle", &|_, val| { if val.len() < 2 { flag.replace_with(|_| val.to_string() ); }}),
], &args);
assert_eq!(flag.into_inner(), "9");