Rust

官网: https://www.rust-lang.org

版本:nightly、beta、stable

如何设计语言的讨论:https://github.com/rust-lang/rfcs

标准库文档:https://doc.rust-lang.org/std/

Rust安装

官方的安装文档:https://www.rust-lang.org/tools/install

一般有两种安装方式:手动下载安装与使用Rustup工具安装,推荐使用第二种方式安装。

Rustup下载地址:https://rustup.rs/

windows下可以双击运行,linux 下可以执行 curl https://sh.rustup.rs -sSf | sh

u@DESKTOP-JSC6PHA:/mnt/d/code/rust_code$ 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. It will add the cargo, rustc, rustup and other commands to Cargo's bin
directory, located at: /home/u/.cargo/bin This path will then be added to your PATH environment variable by modifying the
profile file located at: /home/u/.profile You can uninstall at any time with rustup self uninstall and these changes will
be reverted. Current installation options: default host triple: x86_64-unknown-linux-gnu
default toolchain: stable
modify PATH variable: yes 1) Proceed with installation (default) # 继续安装 (默认)
2) Customize installation # 自定义安装
3) Cancel installation # 取消安装
>1 info: syncing channel updates for 'stable-x86_64-unknown-linux-gnu'
info: latest update on 2019-02-28, rust version 1.33.0 (2aa4c46cf 2019-02-28)
info: downloading component 'rustc'
84.7 MiB / 84.7 MiB (100 %) 13.1 MiB/s ETA: 0 s
info: downloading component 'rust-std'
56.8 MiB / 56.8 MiB (100 %) 13.5 MiB/s ETA: 0 s
info: downloading component 'cargo'
info: downloading component 'rust-docs'
info: installing component 'rustc'
84.7 MiB / 84.7 MiB (100 %) 9.1 MiB/s ETA: 0 s
info: installing component 'rust-std'
56.8 MiB / 56.8 MiB (100 %) 10.5 MiB/s ETA: 0 s
info: installing component 'cargo'
info: installing component 'rust-docs'
6.8 MiB / 8.5 MiB ( 80 %) 16.0 KiB/s ETA: 1.8390096028645833 min 50.3405761718
8.5 MiB / 8.5 MiB (100 %) 153.6 KiB/s ETA: 0 s
info: default toolchain set to 'stable' stable installed - rustc 1.33.0 (2aa4c46cf 2019-02-28) Rust is installed now. Great! To get started you need Cargo's bin directory ($HOME/.cargo/bin) in your PATH
environment variable. Next time you log in this will be done automatically. To configure your current shell run source $HOME/.cargo/env

安装完成后,在$HOME/.cargo/bin文件夹下可以看到如下的可执行程序(该目录默认会自动添加环境变量).

u@DESKTOP-JSC6PHA:~/.cargo/bin$ ls -al
total 126528
drwxrwxrwx 1 u u 512 Apr 11 22:41 .
drwxrwxrwx 1 u u 512 Apr 11 22:43 ..
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo # 包管理器
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-clippy #
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-fmt # 源代码格式化工具
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 cargo-miri
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 clippy-driver
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rls # 为编辑器准备的代码提示工具
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rust-gdb # 调试器
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rust-lldb # 调试器
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustc # 编译器
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustdoc # 文档生成器
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustfmt # .rs文件格式化工具
-rwxr-xr-x 12 u u 10794568 Apr 11 22:41 rustup # 管理这套工具链下载更新的工具

检查是否安装成功,使用rustc -V命令查看版本信息

u@DESKTOP-JSC6PHA:~/.cargo$ rustc -V
rustc 1.33.0 (2aa4c46cf 2019-02-28)

常用命令

rustup self update    # 更新rustup
rustup self uninstall # 卸载rust所有程序
rustup update # 更新工具链 rustup install nightly # 安装nightly版本的编译工具链
rustup default nightly # 设置默认工具链是nightly版本

Rust Language Server

RLS是官方提供的一个标准化的编辑器增强工具。

项目地址:https://github.com/rust-lang-nursery/rls

安装方法

rustup self update
rustup update nightly
# 安装RLS
rustup component add rls --toolchain nightly
rustup component add rust-analysis --toolchain nightly
rustup component add rust-src --toolchain nightly

使用国内的镜像加速访问:

1. 加速rustup

export RUSTUP_DIST_SERVER=https://mirrors.ustc.edu.cn/rust-static
export RUSTUP_UPDATE_ROOT=https://mirrors.ustc.edu.cn/rust-static/rustup

2. 加速cargo (cargo是一个包管理工具,类似于python的pip)

在$HOME/.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"

Hello World

1. 新建hello_world.rs文件,用文本编辑器打开并输入以下内容

// hello_world.rs
fn main() {
let s = "hello world! ";
println!("{}",s);
}

2. 编译及运行

使用rustc hello_world.rs命令,命令执行成功后,会生成一个hello_world的可执行程序,然后执行这个程序,效果如下

3. hello_world代码分析

  • 一般Rust源代码的后缀使用.rs表示 (源码注意使用utf-8编码)
  • 单行注释 //
  • fn 是一个关键字,函数定义必须以 fn 开头。函数体用大括号来包含。
  • 默认情况下,main函数是可执行程序的入口点,它是一个无参数、无返回值的函数
  • 局部变量使用 let 开头,双引号包含起来的部分是字符串常量。
  • 每条语句使用分号结尾
  • 使用println!宏来进行简单的标准输出

00_Rust安装及Hello World的更多相关文章

  1. docker——容器安装tomcat

    写在前面: 继续docker的学习,学习了docker的基本常用命令之后,我在docker上安装jdk,tomcat两个基本的java web工具,这里对操作流程记录一下. 软件准备: 1.jdk-7 ...

  2. 网络原因导致 npm 软件包 node-sass / gulp-sass 安装失败的处理办法

    如果你正在构建一个基于 gulp 的前端自动化开发环境,那么极有可能会用到 gulp-sass ,由于网络原因你可能会安装失败,因为安装过程中部分细节会到亚马逊云服务器上获取文件.本文主要讨论在不变更 ...

  3. Sublime Text3安装JsHint

    介绍 Sublime Text3使用jshint依赖Nodejs,SublimeLinter和Sublimelinter-jshint. NodeJs的安装省略. 安装SublimeLinter Su ...

  4. Fabio 安装和简单使用

    Fabio(Go 语言):https://github.com/eBay/fabio Fabio 是一个快速.现代.zero-conf 负载均衡 HTTP(S) 路由器,用于部署 Consul 管理的 ...

  5. gentoo 安装

    加载完光驱后 1进行ping命令查看网络是否通畅 2设置硬盘的标识为GPT(主要用于64位且启动模式为UEFI,还有一个是MBR,主要用于32位且启动模式为bois) parted -a optima ...

  6. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part3:db安装和升级 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 5.安装Database软件 5. ...

  7. Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作

    Linux平台 Oracle 10gR2(10.2.0.5)RAC安装 Part1:准备工作 环境:OEL 5.7 + Oracle 10.2.0.5 RAC 1.实施前准备工作 1.1 服务器安装操 ...

  8. 【原】nodejs全局安装和本地安装的区别

    来微信支付有2年多了,从2年前的互联网模式转变为O2O模式,主要的场景是跟线下的商户去打交道,不像以往的互联网模式,有产品经理提需求,我们帮忙去解决问题. 转型后是这样的,团队成员更多需要去寻找业务的 ...

  9. tLinux 2.2下安装Mono 4.8

    Tlinux2.2发行版基于CentOS 7.2.1511研发而成,内核版本与Tlinux2.0发行版保持完全一致,更加稳定,并保持对Tlinux2.0的完全兼容.Mono 4版本要求CentOS 7 ...

随机推荐

  1. linux驱动之LED驱动_1

    步骤: 1.框架 2.完好硬件的操作: a.看原理图.引脚 b.看2440手冊 c.写代码: IO口须要用ioremap映射 我的板子电路例如以下所看到的 1.配置GPBCON 寄存器,配置输出   ...

  2. CentOS6.5下用Git克隆代码(https方式)

    一.首先最好保证GIT是最新版 查看GIT命令 $ git --version 有关git的安装,应该有好多文章介绍.注意更新之后,要重启系统,否则显示的版本号,还是老版本. 二.如果工作环境存在网络 ...

  3. research plan1111

    Hello prof.Choi 感谢您的来电,与您的这次通话我已经期盼了很久.我来做个自我介绍,我叫陈飞,今年27岁了,是河北地质大学计算机科学专业的本科毕业生.我非常想提高自己的学历,现在经过刘老师 ...

  4. .NET平台下Redis使用(二)【StackExchange.Redis学习】

    Program.cs内容: using Newtonsoft.Json; using StackExchange.Redis; using System; using System.Data; usi ...

  5. 【HNOI 2003】 激光炸弹

    [题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1218 [算法] 二维前缀和 [代码] #include<bits/stdc++ ...

  6. 如何用css约束一个层不可见

    两种方式: 方式一:设置属性值为none不可见:display:none 这个属性改变了一个元素的显示效果.之前我有提到一点,假如元素使用了none值,那么元素直接干净利落的消失不见.你在右键审查元素 ...

  7. 马拉车算法(Manacher's Algorithm)

    这是悦乐书的第343次更新,第367篇原创 Manacher's Algorithm,中文名叫马拉车算法,是一位名叫Manacher的人在1975年提出的一种算法,解决的问题是求最长回文子串,神奇之处 ...

  8. JavaScript中对象转换为原始值的规则

    JavaScript中对象转换为原始值遵循哪些原则? P52 对象到布尔值对象到布尔值的转换非常简单:所有的对象(包括数字和函数)都转换为true.对于包装对象亦是如此:new Boolean(fal ...

  9. Java里边什么是值传递和引用传递?两个有什么区别

    学过java基础的人都知道,在java中参数的传递过程中有值传递和应用传递,那么这两个到底有什么区别呢,下面我通过例子为大家详细的介绍下. 我们都知道Java中有八种数据类型,基础数据类型分别是:by ...

  10. Zookeeper概念学习系列之zookeeper的节点

    znode有两种类型:  临时节点(ephemeral  node) 和 持久节点(persistent node). znode的类型在创建时确定并且之后不能再修改. 短暂znode的客户端会话结束 ...