rustconventionsprintln

Better practice for double or more line breaks in Rust?


I want double like breaks like below:

[ your title there ]

some kind of content...

In rust, there're two macros to print to stdin, one with like break and one with no like break. And some possible codes to print like above it following:

// use println! only for consistency.

println!("[ your title here ]\n");        // i find it awkward because prinln! already has some sort of '\n' in it.
println!("some kind of content...");

or

// use print! only for consistency.

// but still find it awkward because println! is usually used for single like break
// and this section of code may be the only place where print! is used to print single like break.
print!("[ your title here ]\n\n");
print!("some kind of content...\n");

Is there a better way to print double or more line breaks in Rust? Or other languages that have print with no like break and print with single like break as separeate functions like C#, Java, ..., etc.?


Solution

  • How about

    println!("[ your title here ]");
    println!();
    println!("some kind of content...");
    

    Your code has the same layout and number of line breaks as the resulting output, which makes it obvious, at a glance, where the line breaks are.