1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32




/// pad a string to the left with ``pad`` spaces
pub fn left_pad(s: &str, pad: u32) -> String
{
    left_pad_char(s, pad, ' ')
}

/// pad a string to the left with ``pad`` ``padchar``
pub fn left_pad_char(s: &str, pad: u32, padchar: char) -> String
{
    let mut out = String::new();

    for _ in 0..pad {
        out.push(padchar);
    }
    out.push_str(s);

    out
}

#[cfg(test)]
#[test]
fn pad_test() {

    assert_eq!(left_pad("foo", 2), "  foo");
    assert_eq!(left_pad_char("foo", 2, 'X'), "XXfoo");
    assert_eq!(left_pad_char("bar", 5, '-'), "-----bar");
}