Rust 1.2.0 Translation Website Summary (i)

Source: Internet
Author: User
Tags rust programming language

IntroducedRust programming language

Welcome to learn this tutorial! This tutorial will teach you how to use the Rust programming language. Rust is a system programming language that emphasizes security, performance, and concurrency. It does not even have a garbage collector in order to achieve these goals. This also enables Rust to be applied to areas that other languages cannot do: embedding in other languages, programs that specify space and time requirements, and writing underlying code such as device drivers and operating systems. For other current programming languages, Rust does not have runtime (runtime) and no data competition. Rust is also committed to achieving the "0 cost abstraction", although these abstractions give people a sense of a high-level language. Even so, Rust can still do precise control like a low-level language.

The Rust programming language is divided into seven parts. The introduction of this article is the first one. After this:

    • Get started-set up your PC for Rust development.

    • Learn rust-learn rust programming with small projects.

    • Efficient rust-Learn some high-level concepts for writing excellent rust code.

    • Syntax and semantics-each part of Rust is broken down into small pieces to explain.

    • Daily Rust-has not yet built some of the stable high-end features.

    • Terminology-related reference accounts for this tutorial.

    • Academic research-some writings that affect Rust.

After reading this article, you will want to learn about "rust" or "grammar and Semantics", depending on your preferences: if you want to try a project , you can learn the "learn Rust" chapter, or if you like to start with a small part and thoroughly learn a concept before moving to the next concept, Then you can learn the "syntax and Semantics" section. A rich cross-union enables these parts to be connected together.

Contribution

The source files for this tutorial can be found on Github : GITHUB.COM/RUST-LANG/RUST/TREE/MASTER/SRC/DOC/TRPL

A brief introduction to Rust

Is Rust a language you might be interested in? Let's take a look at some small code examples that show some of their advantages.

One of the main concepts that makes Rust unique is the concept of name ownership. Consider the following small example:

fn Main () {let mut x = vec!["    Hello "," World "); }

This program has a variable binding name of X. The value of this binding is a   vec<t> , which is a ' vector ' created by a macro defined in the standard library. This macro is called VEC, we use! Call the macro with. This follows the general principle of Rust: make things clear. Macros can effectively do things that are more complex than function calls, so they look different in appearance. This symbol! It also helps to parse and make the code easier to write, which is also important.

We use Mut to make x variable: By default, bindings in Rust are immutable. We will change this vector in the following example.

It is also worth mentioning that here we do not need to label the type: Rust is a static type, we do not need to explicitly label the type. Rust has type inference to balance powerful static types and lengthy dimension types.

rust prefers stack allocation instead of heap allocation: X is placed directly on the stack. However, Vec<t>   type is the space of the vector elements allocated on the heap. If you are not familiar with the differences between them, you can now ignore them, or you can view the chapters "stacks and heaps" to learn more about them. As a system programming language, Rust gives you the ability to allocate memory space, but this is not a big problem at the beginning of our phase.

  malloc   and   Free yourself:   when you need to allocate or free memory, the compiler will statically decide and insert these calling functions. It is human nature to err, but the compiler will never forget the functions that insert these calls.

Let's add another line of code to our example above:

fn Main () {let mut x = vec!["        Hello "," World ");    Let y = &x[0]; }

We're here to introduce another kind of binding   , y . In this case , y   is a "reference" to the first element of the vector. Rust's references are similar to pointers in other languages, but there are additional compile-time security checks. In particular, references and ownership systems interact through "borrowing", rather than by owning it. The difference is that when a reference goes out of scope, it does not release the underlying memory. If so, we will release two times of memory, which is obviously not true!

Let's add the third line of code. The surface is correct, but it causes a compilation error:

fn Main () {let mut x = vec!["        Hello "," World ");        Let y = &x[0];    X.push ("foo"); }

push is a method that attaches an element to the end of a vector group. When we try to compile this program, we get an error:

     error: cannot borrow  ' x '  as mutable because it  is also borrowed as immutable        x.push ( "foo");        ^    note: previous  borrow of  ' x '  occurs here; the immutable borrow prevents     subsequent moves or mutable borrows of  ' x '  until the  borrow ends        let y = &x[0];                  ^     note: previous borrow ends here    fn main ()  {     }    ^ 

rust compiler gave a very detailed error, which was one of them. As explained in the error explanation, although our bindings are mutable, we still cannot invoke the push method. This is because we already have a reference to a vector element   , y . When another reference is present, it is dangerous to change certain variables because we may cause the reference to be invalid. In this particular case, when creating a vector, we may have only allocated three elements of space. Adding a fourth element means that all these elements will be allocated a new chunk of memory, while copying the old value into the new memory, updating the internal pointer to the new memory. There's nothing wrong with all this work. The problem is that Y won't update, so we'll have a "dangling pointer". That's not right. In this case, any use of reference y will result in an error, so the compiler has captured this error for us.

So, how do we solve this problem? There are two ways that we can use it. The first method, we use a copy instead of a reference:

fn Main () {let mut x = vec!["        Hello "," World ");        Let y = X[0].clone ();    X.push ("foo"); }

By default, Rust has move semantics, and we can call the Clone () method if we want to replicate some data. In this example   , y   is no longer a reference to the vector stored in X, but rather a copy of its first element, "Hello". Now that there is no reference, our push () method works well.

If we really want to use a reference, we need another option: Make sure our reference jumps out of scope before we try to make a change. The wording looks like this:

fn Main () {let mut x = vec!["        Hello "," World ");        {Let y = &x[0];    } x.push ("foo"); }

We created an inner scope with a set of extra curly braces. Before we call push() , y already jumps out of its scope. So it's doable.

The concept of ownership not only helps prevent dangling pointers, but also helps to solve all the problems associated with them, such as iterator invalidation, concurrency, and so on.

Rust Escrow Address: http://wiki.jikexueyuan.com/project/rust/introduction.html

Rust 1.2.0 Translation Website Summary (i)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.