Rust Guide into the pit: pit to the main drive

Welcome to join me into the pit Rust, since I was the main pit, I was responsible for digging in the front, you can see above, there can also be itchy and I dig together. The pit in the end how deep? I do not know how deep I was holding the mentality to dig deep to come, but first let me jump, you feel free.

About Rust

It is well known in programming languages, high-level language easier to read and control the underlying resource of low-level language is a contradiction. Rust want to challenge the status quo, it attempts to provide a better experience for developers while giving permission to developers control the underlying details (such as memory usage).

Low-level language is prone to all kinds of subtle errors in the development process, but they may be difficult to find a huge impact. Most of the other low-level language can only rely on a wider coverage of test cases and experienced developers to solve these problems. The Rust rely on strict compiler to put an end to these problems.

Ps: the future will be an insight into the Rust compiler of "powerful"

Rust of some tools:

  • Cargo, dependencies and build management tools that can help you relieve add, compile and manage dependencies of pain
  • Rustfmt, to ensure consistency for developers coding style
  • Rust language server supports integrated IDE (I use IDEA)

Installation Rust

If your operating system is Linux or macOS, execute commands in a terminal

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

Option to use the default installation process like (all the way round) until the following message appears when the installation was successful.

Rust is installed now. Great!

The installation script will automatically be added Rust to your PATH environment variable, you can restart the terminal or manually execute the add command to take effect.

$ source $HOME/.cargo/env

Of course, you can add to your .bash_profile file:

$ export PATH="$HOME/.cargo/bin:$PATH"

Finally, execute the following command to check whether the installation was successful Rust

$ rustc --version

In addition, when you try to compile the code Rust, but reported the wrong linker unenforceable, you need to manually install a linker, C compiler will typically contain the correct linker. Some public Rust package will depend on C language code and compiler. So it is best to install a now.

IDEA integrated Rust

IDEA integrated Rust is also very simple, just need to search in Rust Preference-> Plugins, a reboot after installing the plug-Rust IDEA on it.

Hello World

Went to the classic Hello World time, this time I do not want to direct a simple print is over, we mentioned at the beginning of the Cargo is dependent Rust package management tool, so I want to experience Cargo usage.

First create a new project, IDEA can be used directly in the new project, you can use the command Cargo

cargo new hello-world
cd hello-world

New good project after its long structure like this

rust-new-project

among them

  • main.rs is the entrance to our code file
  • Rust Cargo.toml recording metadata file, including dependency.
  • Cargo.lock is recording increased reliance log files can not be modified manually.

Then we add a dependency we need in Cargo.toml file

[dependencies]
ferris-says = "0.1"

IDEA time dependencies will be automatically installed, if not installed, the command may also be performed manually install

cargo build

After rely on installed, you can start writing code:

use ferris_says::say;
use std::io::{stdout, BufWriter};

fn main() {
    let stdout = stdout();
    let out = b"Hello World!";
    let width = 12;

    let mut writer = BufWriter::new(stdout.lock());
    say(out, width, &mut writer).unwrap();
}

Results of the

----------------
| Hello World! |
----------------
              \
               \
                  _~^~^~_
              \) /  o o  \ (/
                '_   -   _'
                / '-----' \

Yes, this is a small crab, as to who it is, take a look at the official explanation

Ferris is the unofficial mascot of the Rust Community. Many Rust programmers call themselves “Rustaceans,” a play on the word “crustacean.” We refer to Ferris with the pronouns “they,” “them,” etc., rather than with gendered pronouns.

Ferris is a name playing off of the adjective, “ferrous,” meaning of or pertaining to iron. Since Rust often forms on iron, it seemed like a fun origin for our mascot’s name!

You can find more images of Ferris on http://rustacean.net/.

About toml file may be some readers are not familiar with (in fact, I am also not familiar with), here briefly about it, its full name is "Tom's Obvious, Minimal Language" is a configuration file format. It's semantics are more obvious, so easy to read. Meanwhile format can be explicitly mapped to the hash table, so it can be easily resolved in several languages.

GitHub address is: https://github.com/toml-lang/toml

Interested students can do more in-depth understanding.

postscript

At this point, I'm sure they have jumped in, and had wanted to follow up on the concerns I remember a friend oh.

Guess you like

Origin www.cnblogs.com/Jackeyzhe/p/11657966.html