Rust by Example | страница 2



>// There are two slashes at the beginning of the line

>// And nothing written inside these will be read by the compiler

>// println!("Hello, world!");

>// Run it. See? Now try deleting the two slashes, and run it again.

>/*

>* This is another type of comment, a block comment. In general,

>* line comments are the recommended comment style. But

>* block comments are extremely useful for temporarily disabling

>* chunks of code. /* Block comments can be /* nested, */ */

>* so it takes only a few keystrokes to comment out everything

>* in this main() function. /*/*/* Try it yourself! */*/*/

>*/

>/*

>Note: The previous column of `*` was entirely for style. There's

>no actual need for it.

>*/

>// You can manipulate expressions more easily with block comments

>// than with line comments. Try deleting the comment delimiters

>// to change the result:

>let x = 5 + /* 90 + */ 5;

>println!("Is `x` 10 or 100? x = {}", x);

>}

>הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

>XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Printing is handled by a series of macros defined in std::fmt some of which include:

   • format!: write formatted text to String

   • print!: same as format! but the text is printed to the console (io::stdout).

   • println!: same as print! but a newline is appended.

   • eprint!: same as format! but the text is printed to the standard error (io::stderr).

   • eprintln!: same as eprint!but a newline is appended.

All parse text in the same fashion. As a plus, Rust checks formatting correctness at compile time.

>fn main() {

>// In general, the `{}` will be automatically replaced with any

>// arguments. These will be stringified.

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

>// Without a suffix, 31 becomes an i32. You can change what type 31 is

>// by providing a suffix. The number 31i64 for example has the type i64.

>// There are various optional patterns this works with. Positional

>// arguments can be used.

>println!("{0}, this is {1}. {1}, this is {0}", "Alice", "Bob");

>// As can named arguments.