use std::{thread, time,};
use std::io::{self, Write};
#[cfg(test)]
mod tests {
use ::*;
#[test]
fn it_works() {
let now = time::Instant::now();
let sample_text = String::from("few character string 👍");
print_string_with_delay(
&sample_text,
time::Duration::from_millis(10)).expect("panicking");
assert!(now.elapsed() >= time::Duration::from_millis(10 * sample_text.chars().count() as u64));
}
}
pub fn print_string_with_delay(word: &String, delay: time::Duration) -> io::Result<()> {
for line in word.lines() {
for letter in line.chars() {
let mut string = String::new();
string.push(letter);
io::stdout().write(string.as_bytes())?;
io::stdout().flush()?;
thread::sleep(delay);
}
io::stdout().write(b"\n")?;
io::stdout().flush()?;
}
Ok(())
}
pub fn print_slice_with_delay(word: &str, delay: time::Duration) -> io::Result<()> {
print_string_with_delay(&String::from(word), delay)
}