Features and basic configuration of trust language

Time:2021-10-20

Rust is a system level programming language launched by Mozilla. Its two characteristics are zero overhead abstraction and security.
When I was in college, I began to notice the dynamics of the trust language.
At that time, I was gnawing at a large c + + book “C + + primer”, and felt that trust targeted the pain of C + + everywhere.
Learning trust enables you to program with features that are not inferior to C + +, without spending too much energy in various special cases and details of syntax.

Good programmers need to master multiple languages to learn different ways of thinking. For system level languages, trust is excellent,
Recently, I have a little time to learn this language which has been my birthday for a long time.

<!–more–>

Features of trust

When I was in college, I began to notice the dynamics of the trust language. At that time, I was gnawing at a big C + + book “C + + primer”.

C + + is quite difficult to learn because of its historical legacy and compatibility with C, resulting in poor syntax orthogonality and unity,
There are a lot of grammar points to learn and pit points to avoid. Even some students are “C + + from getting started to giving up”. (me too)
The rest of the students who say they use C + + are actually C with class. They don’t dare to use too advanced features. They’re afraid they can’t hold…

It has the advantages of C + + as a system level language

Similar to C + +, rust is positioned as a system level language. Therefore, it has all the characteristics of C + + as a system level language:

  1. Zero overhead abstraction。 Abstraction does not introduce additional overhead, and the compiler compiles very high-performance machine code.
  2. No GC。 System level language needs to be applied to scenes with high real-time requirements, and GC cannot be used. It is also used to write the underlying infrastructure. There is no need to use GC.
  3. The memory model is not highly encapsulated. Like C + +, you can assign objects to the stack or to the heap.
    For system level languages, these fine-grained control forces are required, rather than high-level languages such as Java. Java can be used only by knowing the concept of reference, and there is no need to care about memory allocation.

It has stronger static type checking and modern language features

In addition, besides the advantages of C + +, it also has:

  1. Syntax clean orthogonality, not as cumbersome as C + +.
  2. Security。 The rust syntax introduces the concepts of ownership and lifetime, so that some memory management errors can be detected at compile time,
    This is a killer feature of trust.
  3. Characteristics of modern language。 Language features absorb a large number of excellent features of modern language and support multi paradigm.

There is no historical legacy of C + +

From the information I have learned, there are also the following features of benchmarking C + +. For a beginner, I don’t feel very much at present:

  1. The modern modular system is much better than the ancient way of inheriting C language (using #include precompile and the concept of compilation unit) in C + +.
  2. Better generics. Easier to use than C + + templates.

Construction of trust development environment

Step 1: install the trust tool chain:
The official website recommends:

curl https://sh.rustup.rs -sSf | sh

This installs the trust toolchain to~/.cargo/binDirectory, including rustc, cargo and rustup.

I recommend climbing the wall to download. I choose proxychains:

proxychains curl https://sh.rustup.rs -sSf > tmp.sh
sh tmp.sh

Step 2: install the IDE.
I recommend using the trust plug-in launched by JetBrains. Just find an IDE to install JetBrains.
As a Java coder, I choose idea. It’s as easy as putting an elephant in the refrigerator. It’s just three simple steps:

  1. Open IntelliJ idea.
  2. Settings – > plugins, install the trust plug-in.
  3. Settings – > Languages & Frameworks – > rust configure Standard Library paths and other options.

OK, create a new project, and you will find one more trust option.

As a beginner, don’t spend too much energy on the tool chain. First, you should quickly set up an environment, and then write a few demos using grammar.

First trust program

As the first trust program, let’s first choose sorting:

fn selection_sort(nums: &mut Vec<i32>) {
    for i in 0.. nums.len() {
        let mut min = i;
        for j in i.. nums.len() {
            if nums[j] < nums[min] {
                min = j;
            }
        }
        nums.swap(i, min);
    }
}

The syntax in trust is not C-system syntax, so you need to practice writing code to be familiar with the syntax of trust. The syntax involved in the above code is:

  1. First, use the keyword let to declare variables, and trust encourages automatic type derivation.
  2. By default, variables are immutable. If you need to make the variable variable variable, you need to explicitly declaremut。 Trust encourages the use of immutable variables.
    You can also specify the type, but unlike the C-system syntax, it is marked after the variable. For people who are used to C-system syntax (C, Java, JS), they are not used to…
  3. C system syntax does not need to define the keyword of the function, and the return type is at the beginning. Unlike this, trust uses FN as the keyword to define the function.
  4. There are also while loops and for loops in trust. Unlike the C-system syntax, the for loop is used to iterate over the list. If you need to iterate over a number range,
    You can use(1..10), it actually creates aRange<i32>Type and iterate.

Next is the auxiliary function to convert vector to string:

fn vector_to_string(nums: &Vec<i32>) -> String {
    let mut s = String::new();
    for i in nums.iter() {
        s += &(i.to_string() + " ");
    }
    return s;
}
  1. String::newIt implies that there is no constructor and new operator in trust. How to create objects? Use static factory functions.
    This design saves a lot of trouble. First, even if you give you a constructor, you will encapsulate a layer of various factories in engineering practice.
    Second, you won’t create an object that is half initialized. Such as this reference disclosure in Java.
  2. String object support+=Operation, which indicates that rust supports operator overloading.

Finally, the main function:

fn main() {
    let mut v = vec![9, 2, 5, 4, 8, 12, 1];
    selection_sort(&mut v);
    println!("{}", vector_to_string(&v));
}
  1. vec!In fact, it is a macro. Trust supports powerful macros, which are as powerful as lisp.
  2. &Is a reference, involving the concepts of ownership and borrowing.

Learning methods and materials

For programmers with a certain foundation, the most efficient way to learn a language is:

  1. Combined with the ready-made sample program, learn the grammar tutorial.
  2. Write a summary, summarize the grammar according to your own understanding, and take notes.
  3. Write demos and make wheels. Find some typical topics and write small demos and wheels to try.

Finally, there are some materials worth learning:

  1. Trust official documentation:https://www.rust-lang.org/zh-…
    Learn the main materials of trust.
  2. Learning rust by examplehttps://rustcc.github.io/rust…
    Make up for the shortcomings of the official documents of rust, and refer to some examples of syntax collocation.
  3. Awesomes series of trust.https://github.com/rust-unoff…

last

Everyone has different views on the understanding of programming language.

Personally, I prefer the metaphor in the code book, that is, programming language is a tool in the programmer’s toolbox,
Generally speaking, it uses good design as much as possible to avoid additional burden on programmers, and has powerful features that enable programmers to gracefully realize various requirements.
It includes unified orthogonal syntax, excellent type system, appropriate syntax sugar that does not destroy unity, etc.

Recommended Today

SQL exercise 20 – Modeling & Reporting

This blog is used to review and sort out the common topic modeling architecture, analysis oriented architecture and integration topic reports in data warehouse. I have uploaded these reports to GitHub. If you are interested, you can have a lookAddress:https://github.com/nino-laiqiu/TiTanI recorded a relatively complete development process in my hexo blog deployed on GitHub. You can […]