Function left_pad::leftpad_with [] [src]

pub fn leftpad_with<'a, S>(string: S, codepoints: usize, pad_char: char) -> Cow<'a, str> where S: Into<Cow<'a, str>>

Pads a string to the given number of chars by inserting the character pad_char from the left.

If the given string has more than or exactly the desired number of codepoints, it will be returned as-is.

Codepoints are not graphemes, so the result might always not be what a human would expect.

Examples

use left_pad::leftpad_with;

assert_eq!(leftpad_with("blübb", 7, ' '), "  blübb");
assert_eq!(leftpad_with("blübb", 7, '→'), "→→blübb");

assert_eq!(leftpad_with("blübb", 5, ' '), "blübb");
assert_eq!(leftpad_with("blübb", 3, ' '), "blübb");

assert_eq!(leftpad_with("čömbiñiñg märks", 22, ' '),  "  čömbiñiñg märks");