fn pad_both(s: &str, total_len: usize, pad_char: char) -> String {
if s.len() >= total_len {
return s.to_string();
}
let pad_total = total_len - s.len();
let pad_left = pad_total / 2;
let pad_right = pad_total - pad_left;
format!(
"{}{}{}",
pad_char.to_string().repeat(pad_left),
s,
pad_char.to_string().repeat(pad_right)
)
}
fn main() {
println!("'{}'", pad_both("Rust", 9, ' '));
println!("{}", pad_both("Rust", 9, '*'));
}
/*
run:
' Rust '
**Rust***
*/