[Learn Rust together] Preparation before Rust learning – annotation and formatted output

Hits: 0

Tip: Prepare to warm up.

Article directory


Before we officially start learning [Rust] , we need to know a little about some basic Rust operations, because we don’t know these things. When I read the following articles, I was confused by the sudden basic grammar. I read it when I was learning Rust. It’s the Rust Bible. Although it said formatted output when it was mentioned, it suddenly felt awkward when I saw it. I always felt that the study was not systematic, and we were all learning in a few words. Therefore, before we officially entered the study of Rust, we First explain some basic output and comments.

1. Notes

Comments are essential to any program, and Rust supports several different styles of comments.

1. General Notes

Ordinary comments are comments whose content will be ignored by the compiler. Generally, there are two usages, line comments and block comments.

// Single-line comment, the content of the comment is until the end of the line. 
/* Block comment, comment content up to the closing delimiter. */

2. Documentation Notes

A document comment is a comment whose content is parsed into an HTML document, which also has two uses.

/// Generate help documentation for the following items. 
//! Generate help documentation for the item the comment belongs to.

We will introduce the documentation comments here first. After we have a deeper understanding of Rust, we will introduce the documentation comments in detail. We mostly use ordinary comments in the learning process.

2. [Formatted output]

Printing is handled by a series of macros defined in std::fmt, including:

  • format!: Write formatted text to a string.
  • print!: Like format!, but outputs text to the console (io::stdout).
  • println!: Like print!, but appends a newline to the output.
  • eprint!: Like print!, but prints text to standard error (io::stderr).
  • eprintln!: Similar to eprint!, but appends a newline to the output.

These macros all parse text in the same way. An added advantage is that the correctness of formatting is checked at compile time.

output [string]

Just like other programming languages, Rust outputs a string by simply adding the string to be output to the parameter.

println!("7 days");

output a string with placeholders

Typically, {}it will be replaced by the contents of any variable. The variable content will be converted to a string.

println!("{} days", 31);

Multiple placeholders can be used,
The placeholder can also choose the number of variable positions,

println !( "{0}, this is {1}. {1}, this is {0}" , "Zhang San" , "Li Si" );

You can also name the parameters. In the following example, it objectrepresents the object, subjectthe subject, and verbthe predicate .

Output a string with placeholders in the specified format

Special formats can be specified :after .

println !( "The binary representation of {} is: {0:b}" , 10);

Specify width alignment

Rust’s aligned output needs to specify a width. The code is as follows,

// left-aligned 
println!( "{number:<width$}" , number= 1 , width= 6 );
 // right-aligned 
println!( "{number:>width$}" , number= 1 , width= 6 );

0 for missing digital output

Here is a comparison with the above

println!("{number:>0width$}", number=1, width=6);

The bottom is to add 0, the top is alignment


The above is the content of this time. This article mainly talks about some basic content of annotation and formatted output.


  • common note
  • Documentation Notes

formatted output

  • output string
  • output a string with placeholders
  • output a string specifying style placeholders
  • Specify width alignment
  • Fill in the absence of 0

In the comments , we leave the documentation comments for later, because if you are not familiar with the basic syntax, it will be very confusing to write documentation comments.

We only introduce the basics of formatted output . There will be many advanced usages that need to understand the basic concepts of Rust before they can be learned. We will intersperse the introduction in the article, and we will dedicate a chapter to more details later. Describes the functions for formatting output.

Please look forward to more exciting content in the future.

You may also like...

Leave a Reply

Your email address will not be published.