Выбрать главу

Rust is a modern systems programming language focusing on safety, speed, and concurrency. It accomplishes these goals by being memory safe without using garbage collection.

Rust by Example (RBE) is a collection of runnable examples that illustrate various Rust concepts and standard libraries. To get even more out of these examples, don't forget to install Rust locally and check out the official docs. Additionally for the curious, you can also check out the source code for this site.

Now let's begin!

This is the source code of the traditional Hello World program.

// This is a comment, and is ignored by the compiler

// You can test this code by clicking the "Run" button over there ->

// or if you prefer to use your keyboard, you can use the "Ctrl + Enter" shortcut

// This code is editable, feel free to hack it!

// You can always return to the original code by clicking the "Reset" button ->

// This is the main function

fn main() {

// Statements here are executed when the compiled binary is called

// Print text to the console

println!("Hello World!");

}

הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה

XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

println! is a macro that prints text to the console.

A binary can be generated using the Rust compiler: rustc.

$ rustc hello.rs

rustc will produce a hello binary that can be executed.

$ ./hello

Hello World!

Click 'Run' above to see the expected output. Next, add a new line with a second println! macro so that the output shows:

Hello World!

I'm a Rustacean!

Any program requires comments, and Rust supports a few different varieties:

   • Regular comments which are ignored by the compiler:

      • // Line comments which go to the end of the line.

      • /* Block comments which go to the closing delimiter. */

   • Doc comments which are parsed into HTML library documentation:

      • /// Generate library docs for the following item.

      • //! Generate library docs for the enclosing item.

fn main() {

// This is an example of a line comment

// 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

Library documentation

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.

println!("{subject} {verb} {object}",

object="the lazy dog",

subject="the quick brown fox",