[−][src]Function tweetr::util::prompt_exact_len
pub fn prompt_exact_len<R, W, F>(
input: &mut R,
output: &mut W,
prompt_s: &str,
verifier: F,
desired_len: usize
) -> IoResult<String> where
R: BufRead,
W: Write,
F: Fn(&String) -> bool,
Ask the user to input a string of the exact length of desired_len
, (re)prompting as necessary.
Examples
Allow anything 10 charactes long:
assert_eq!(prompt_exact_len(&mut Cursor::new(b"0123456789"), &mut Vec::new(), "Allowed chars", |_| true, 10).unwrap(), "0123456789".to_string());
Allow a 10-character-long u64
:
assert_eq!(prompt_exact_len(&mut Cursor::new(b"1234567890"), &mut Vec::new(), "Long number", |s| u64::from_str(s).is_ok(), 10).unwrap(), "1234567890".to_string()); assert!(prompt_exact_len(&mut Cursor::new(b"1234abcdef"), &mut Vec::new(), "Long number", |s| u64::from_str(s).is_ok(), 10).is_err());