为什么我建议你学一下 Rust | Rust 初探

简介: 为什么我建议你学一下 Rust | Rust 初探

一、Rust 的特点

Rust 是一种多范式、通用编程语言,旨在提高性能和安全性,尤其是安全并发性。 Rust 在语法上类似于 C++,但可以通过使用借用检查器来验证引用来保证内存安全。 Rust 在没有垃圾收集的情况下实现了内存安全,并且引用计数是可选的。 Rust 被称为系统编程语言,除了函数式编程等高级特性外,它还提供低级内存管理机制。

Rust 首次出现于 2010 年,由 Mozilla Research 的 Graydon Hoare 设计,Dave Herman、Brendan Eich 等人也做出了贡献。 设计人员在编写 Servo 实验性浏览器引擎和 Rust 编译器时改进了语言。 Rust 的主要影响包括 C++、OCaml、Haskell 和 Erlang。 它在行业中获得了越来越多的使用和投资,包括亚马逊、微软、Facebook、Discord 和 Dropbox。

Rust 语言旨在以简单的方式开发可靠且健壮的系统。 Rust 可用于修复高级软件以及硬件特定程序。


二、快速了解

1. 安装环境

MacOS下安装 Rust 很简单。

curl https://sh.rustup.rs -sSf | sh
info: downloading installer
Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
  /Users/xiaoyu/.rustup
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
  /Users/xiaoyu/.cargo
This can be modified with the CARGO_HOME environment variable.
The cargo, rustc, rustup and other commands will be added to
Cargo's bin directory, located at:
  /Users/xiaoyu/.cargo/bin
This path will then be added to your PATH environment variable by
modifying the profile files located at:
  /Users/xiaoyu/.profile
  /Users/xiaoyu/.bash_profile
  /Users/xiaoyu/.zshenv
You can uninstall at any time with rustup self uninstall and
these changes will be reverted.
Current installation options:
   default host triple: x86_64-apple-darwin
     default toolchain: stable (default)
               profile: default
  modify PATH variable: yes
1) Proceed with installation (default)
2) Customize installation
3) Cancel installation
>1

以上我们可以得到的是:

  • 官方的包管理器是 Cargo
  • rust 的安装路径是 /Users/xiaoyu/.cargo/bin
  • 脚本会自动把环境变量添加到配置文件中

经过了一段时间的下载,最后安装完成。

stable-x86_64-apple-darwin installed - rustc 1.62.0 (a8314ef7d 2022-06-27)
Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your PATH environment variable to include
Cargo's bin directory ($HOME/.cargo/bin).
To configure your current shell, run:
source "$HOME/.cargo/env"

2. 查看版本

~ % rustc --version
rustc 1.62.0 (a8314ef7d 2022-06-27)

目前 rust 的版本为 1.62.0

3. Hello World

新建一个文件名为HelloWorld.rs,内容如下

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

main 函数是整个应用程序的入口,fn 这种方法的写法甚至比 go 的 func更短。

执行rustc HelloWorld.rs,对程序进行编译。在 MacOS的环境下,我们可以得到一个HelloWorld的可执行文件。

执行这个文件,我们可以看到输出结果。

rust % ./HelloWorld 
Hello World !!

所以,Rust 应用程序的是需要编译的,和 Java 、Go 是一样的,区别于 Python 、PHP 。

三、总结

Rust 需要编译执行,特点是安全、快速、支持并发。

相关文章
|
2月前
|
Rust 安全 开发者
Rust
Rust
22 0
|
24天前
|
Rust 安全
|
2月前
|
Rust 编译器 Linux
【Rust】——rust前言与安装rust
【Rust】——rust前言与安装rust
32 0
【Rust】——rust前言与安装rust
|
4月前
|
Rust 索引
Rust 编程小技巧摘选(5)
Rust 编程小技巧摘选(5)
41 0
Rust 编程小技巧摘选(5)
|
4月前
|
Go Python Rust
Rust 编程小技巧摘选(7)
Rust 编程小技巧摘选(7)
45 0
Rust 编程小技巧摘选(7)
|
4月前
|
Rust 存储 算法
Rust 编程小技巧摘选(3)
Rust 编程小技巧摘选(3)
24 0
Rust 编程小技巧摘选(3)
|
4月前
|
Rust 索引
Rust 编程小技巧摘选(6)
Rust 编程小技巧摘选(6)
46 1
Rust 编程小技巧摘选(6)
|
4月前
|
Rust
Rust 编程小技巧摘选(8)
Rust 编程小技巧摘选(8)
79 0
Rust 编程小技巧摘选(8)
|
4月前
|
Rust 安全 测试技术
Rust 编程小技巧摘选(4)
Rust 编程小技巧摘选(4)
45 0
Rust 编程小技巧摘选(4)
|
4月前
|
Rust 安全 开发者
一篇学会Rust语言
一篇学会Rust语言
54 0