How to pad a string on the right in Rust

2 Answers

0 votes
fn main() {
    let original = "Rust";
    
    let padded = format!("{:<7}", original); 
    
    println!("'{}'", padded); 
}

 
 
/*
run:
   
'Rust   '
   
*/
 

 



answered Jul 3 by avibootz
0 votes
fn pad_right(s: &str, total_len: usize, pad_char: char) -> String {
    let pad_len = total_len.saturating_sub(s.len());
    
    format!("{}{}", s, pad_char.to_string().repeat(pad_len))
}

fn main() {
    let padded = pad_right("Rust", 7, '*');
    
    println!("{}", padded); 
}

 
 
/*
run:
   
Rust***
   
*/
 

 



answered Jul 3 by avibootz
...