Rust语言

Rust 官方在线工具: Rust PlaygroundA browser interface to the Rust compiler to experiment with the languageicon-default.png?t=M85Bhttps://play.rust-lang.org/

第一个 Rust 程序

Rust 语言代码文件后缀名为 .rs, 如 test.rs

fn main() {
    println!("Hello World!");
}

使用 rustc 命令编译 test.rs 文件:

$ rustc test.rs   # 编译 test.rs 文件

编译后会生成 runoob 可执行文件:

$ ./test    # 执行 test
Hello World!

Rust语言_第1张图片

 

1.1 Rust 语言简介

Rust 是一门系统级别的编程语言。

Rust 由 Graydon Hoare 开发并在被 Mozilla 实验室收购后发扬光大。

 

Rust语言_第2张图片

1.2 应用程序及所采用的语言

总所周知,Java 和 C# 语言 一般用于构建面向用户服务的软件。比如电子表格,文字处理器,Web 应用程序或移动应用程序等业务应用程序。

至于面向机器的那些系统级别应用程序,因为对性能和并发由极高的要求,系统、软件以及软件平台所采用的语言几乎都是清一色的 C 语言 和 C++ 语言,比如它们操作系统,游戏引擎,编译器等。这些编程语言需要很大程度的硬件交互。

系统级别和应用程序级别编程语言面临两个主要问题:

  • 难以写出高安全的语言,尤其是 C/C++ 指针带来的悬空指针,缓冲区溢出和内存泄漏等问题
  • 缺少语言级别的高并发特性。
  • Rust语言_第3张图片

 

1.3 为什么选择 Rust ?

为什么选择 Rust ?

估计我们能说出一大堆理由来,然而,对我来说,最大的理由就是 字节跳动公司的飞聊团队已经用上了 Rust 语言了。这意味着学好 Rust 语言就有机会找到高薪的工作。

此外,正如 Rust 语言自己说的那样,Rust 语言有三大愿景:

  • 高安全
  • 高性能
  • 高并发

Rust 语言旨在以简单的方式开发高度可靠和快速的软件。

Rust 语言支持用现代语言特性来写一些系统级别乃至机器级别的程序。

1.3.1 高性能

高性能是所有语言的最高追求,Rust 也不例外。

为了追求极致的性能,Rust 抛弃了 C/C++ 之外的语言都有的 垃圾回收器( Garbage Collector (GC))。

也就是消除了垃圾回收机制带来的性能损耗。

1.3.2 编译时内存安全

Rust 虽然也有指针的概念,但这个概念被大大的弱化,因此它没有 C/C++ 那种悬空指针,缓冲区溢出和内存泄漏等等问题。

1.3.3 天生多线程安全运行程序

Rust 是为多线程高并发而设计的系统级别语言

Rust 的 拥有者(ownership) 概念和 内存安全 规则使得它天生支持高并发,而且是支持没有数据竞争的高并发。

1.3.4 Rust 语言支持 Web Assembly (WASM) 语言

Rust 的目标是成为高并发且高安全的系统级语言,但这并不代表它就不能开发 Web 应用。

Rust 支持通过把代码编译成 Web Assembly (WASM) 语言从而能够在浏览器端以实现快速,可靠的运行。

Web Assembly (WASM) 语言是被设计用来在浏览器端/嵌入式设别上运行的,用于执行 CPU 计算密集型的语言。

也就是说 Web Assembly (WASM) 语言 的目标是和 Javascript 一样能够在浏览器里运行,但因为是编译型,所以更高效。

学习Rust:学习 Rust - Rust 程序设计语言 一门赋予每个人构建可靠且高效软件能力的语言。https://www.rust-lang.org/zh-CN/learn

Crate std
1.0.0 · source 
The Rust Standard Library
The Rust Standard Library is the foundation of portable Rust software, a set of minimal and battle-tested shared abstractions for the broader Rust ecosystem. It offers core types, like Vec and Option, library-defined operations on language primitives, standard macros, I/O and multithreading, among many other things.

std is available to all Rust crates by default. Therefore, the standard library can be accessed in use statements through the path std, as in use std::env.

How to read this documentation
If you already know the name of what you are looking for, the fastest way to find it is to use the search bar at the top of the page.

Otherwise, you may want to jump to one of these useful sections:

std::* modules
Primitive types
Standard macros
The Rust Prelude
If this is your first time, the documentation for the standard library is written to be casually perused. Clicking on interesting things should generally lead you to interesting places. Still, there are important bits you don’t want to miss, so read on for a tour of the standard library and its documentation!

Once you are familiar with the contents of the standard library you may begin to find the verbosity of the prose distracting. At this stage in your development you may want to press the [-] button near the top of the page to collapse it into a more skimmable view.

While you are looking at that [-] button also notice the source link. Rust’s API documentation comes with the source code and you are encouraged to read it. The standard library source is generally high quality and a peek behind the curtains is often enlightening.

cargo

cargo是一个依赖管理和编译集成工具。

cargo --version

cargo 创建工程

cargo new hello_cargo

cargo.toml是依赖管理文件

编译

cargo build

运行

.\target\debug\hello_cargo.exe
or 
cargo run

发布

cargo build --release

Guessing Game

  1. 创建新项目

    cargo new gassing_game
    
  2. 运行一下

    cargo run
    
  3. 输入输出

    //使用标准库,不需要外部依赖的库
    use std::io;
    fn main() {
        println!("Hello, world!");
        println!("Please input a number!");
    	//定义一个字符串变量
        let mut guess = String::new();
        //获取标准输入
        io::stdin().read_line(&mut guess).expect("Failed to read line");
    	//打印变量
        println!("your input is {}",guess);
    }
    
    
  4. 导入随机数库

    //cargo.toml
    
    [dependencies]
    rand = "0.8.3"
    
  5. 更新镜像源

    在user/.cargo/config
    [source.crates-io]
    registry = "https://github.com/rust-lang/crates.io-index"
    replace-with = 'ustc'
    [source.ustc]
    registry = "git://mirrors.ustc.edu.cn/crates.io-index"
    
  6. 编译,下载依赖

    cargo build
    
  7. 使用随机数

    
    use rand::Rng;
    fn main() {
        let secret_number = rand::thread_rng().gen_range(1..101);
        println!("secret number is {}",secret_number);
    }
    
  8. 比较

    use std::io;
    use rand::Rng;
    use std::cmp::Ordering;
    fn main() {
        println!("Hello, world!");
        let secret_number = rand::thread_rng().gen_range(1..101);
        println!("secret number is {}",secret_number);
        println!("Please input a number!");
        let mut guess = String::new();
        io::stdin().read_line(&mut guess).expect("Failed to read line");
        let guess:u32 = guess.trim().parse().expect("please input a number!");
        println!("You guessed {}",guess);
        match guess.cmp(&secret_number) {
            Ordering::Less => println!("too small!"),
            Ordering::Greater => println!("too big!"),
            Ordering::Equal => println!("You win!"),
        }
    
    }
    
    
  9. 循环

    use std::io;
    use rand::Rng;
    use std::cmp::Ordering;
    fn main() {
        println!("Hello, world!");
        let secret_number = rand::thread_rng().gen_range(1..101);
        // println!("secret number is {}",secret_number);a
        loop {
            println!("Please input a number:");
            let mut guess = String::new();
            io::stdin().read_line(&mut guess).expect("Failed to read line");
            let guess:u32 = guess.trim().parse().expect("please input a number!");
            println!("You guessed {}",guess);
            match guess.cmp(&secret_number) {
                Ordering::Less => println!("too small!"),
                Ordering::Greater => println!("too big!"),
                Ordering::Equal => {
                    println!("You win!");
                    break;
                }
            }
        }
    }
    
    

     

Rust 安装须知

入门

用 rustup 管理工具链

Rust 由工具 rustup 安装和管理。Rust 有着以 6 星期为周期的 快速版本迭代机制,支持 大量平台,因而不同时期存在大量不同的 Rust 构建版本。 rustup 用于管理不同平台下的 Rust 构建版本并使其互相兼容, 支持安装由 Beta 和 Nightly 频道发布的版本,并支持其他用于交叉编译的编译版本。

如果您曾经安装过 rustup,可以执行 rustup update 来升级 Rust。

更多信息请查阅 rustup 文档。

配置 PATH 环境变量

在 Rust 开发环境中,所有工具都安装在 ~/.cargo/bin 目录中,您可以在这里找到包括 rustccargo 和 rustup 在内的 Rust 工具链。

Rust 开发者通常会将该目录加入 PATH环境变量中。在安装过程中,rustup 会尝试配置 PATH。 由于不同平台、命令行 Shell 之间存在差异,rustup 中也可能存在 Bug,因此在终端重启或用户重新登录之前,rustup 对 PATH 的修改可能不会生效,甚至完全无效。

如果安装后在终端尝试执行 rustc --version 失败,那么,以上内容就是最可能的原因。

卸载 Rust

在任何时候如果您想卸载 Rust,您可以运行 rustup self uninstall

参考链接

  • Rust 官方网站:Rust 程序设计语言
  • Rust 官方文档:Learn Rust - Rust Programming Language
  • Rust Play:Rust Playground
  • Visual Studio Code:Visual Studio Code - Code Editing. Redefined

你可能感兴趣的:(rust,开发语言,后端)