Printing a character a variable number of times with println

后端 未结 2 1091
野的像风
野的像风 2021-01-11 22:28

I want to use println! and the powerful formatting tools of format! to print a character a specific number of times. Of course this is possible wit

相关标签:
2条回答
  • 2021-01-11 23:32

    If you want a cleaner way to repeat any Displayable item without creating an intermediate allocation, you can create a wrapper struct and write a custom Display implementation that performs the repetition:

    use std::fmt::{self, Display};
    
    #[derive(Clone, Copy)]
    struct DisplayRepeat<T>(usize, T);
    
    impl<T: Display> Display for DisplayRepeat<T> {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            for _ in 0..self.0 {
                self.1.fmt(f)?;
            }
            Ok(())
        }
    }
    
    fn repeat<T>(times: usize, item: T) -> DisplayRepeat<T> {
        DisplayRepeat(times, item)
    }
    
    fn main() {
        println!("Here is love for you: {}", repeat(10, '♥'));
    }
    
    0 讨论(0)
  • 2021-01-11 23:33

    Solution to your code

    fn give_love(count: usize) {
        println!("Here is love for you: {:♥<1$}", "", count);
    }
    

    Explanation

    You can (mis-)use the fill feature that allows to fill a printed value with some character of your choice. The grammar for this feature alone looks like:

    '{' ':' <fill> <align> <width> '}'
    

    Where width is either a constant number or a reference to an argument of type <argument_index> '$'. So 3 would mean a width of constant 3 and 1$ would mean a width of the value of the 1st argument of println!.


    However: here we are kind of "misusing" this feature and we mustn't forget that we are only specifying the "fill" for some other printable thing, which is passed by argument to println. This can be an empty string though.

    println!("love: {:♥<3}", "");     // love: ♥♥♥
    println!("love: {:♥<1$}", "", 5); // love: ♥♥♥♥♥
    

    Here are some examples where we don't pass an empty string:

    println!("love: {:♥<5}", "#");    // love: #♥♥♥♥
    println!("love: {:♥>5}", "#");    // love: ♥♥♥♥#
    println!("love: {:♥^5}", "#");    // love: ♥♥#♥♥
    
    0 讨论(0)
提交回复
热议问题