Читаем Rust by Example полностью

Rust by Example

https://doc.rust-lang.org/stable/rust-by-example/

Collective of authors

Учебные пособия, самоучители18+

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!

<p id="hello_world"><strong><a l:href="#hello_world">Hello World</a></strong></p>

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!

<p id="activity"><strong><a l:href="#activity">Activity</a></strong></p>

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!

<p id="comments"><strong><a l:href="#comments">Comments</a></strong></p>

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.

*/

Перейти на страницу:

Похожие книги