wordpress   发布时间:2022-04-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了00_Rust安装及Hello World大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述@H_874_4@ 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 一般有两种
@H_616_10@

Rust

官网: https://www.rust-lang.org
版本:nightly、beta、stable
如何设计语言的讨论:https://github.com/rust-lang/rfcs
标准库文档:https://doc.rust-lang.org/std/

Rust安装@H_874_4@

官方的安装文档:https://www.rust-lang.org/tools/install
一般有两种安装方式:手动下载安装与使用Rustup工具安装,推荐使用第二种方式安装。
Rustup下载地址:https://rustup.rs/
windows下可以双击运行,linux 下可以执行 curl https://sh.rustup.rs -sSf | sh

[email protected]:/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文件夹下可以看到如下的可执行程序(该目录认会自动添加环境变量).

@L_502_1@:~/.cargo/bin$ ls -al
@R_920_10586@l 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命令查看版本信息

[email protected]:~/.cargo$ rustc -V
rustc 1.33.0 (2aa4c46cf 2019-02-28)

常用命令@H_874_4@
rustup self update    # 更新rustup
rustup self uninstall       # 卸载rust所有程序
rustup update      # 更新工具链

rustup install nightly    #  安装nightly版本的编译工具链
rustup default nightly    # 设置认工具链是nightly版本

Rust Language Server@H_874_4@

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

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

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@H_874_4@

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

// Hello_world.rs
fn main() {
    let s = "Hello World! ";
    println!("{}",s);
}

2. 编译及运行

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

00_Rust安装及Hello World

3. Hello_world代码分析

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

大佬总结

以上是大佬教程为你收集整理的00_Rust安装及Hello World全部内容,希望文章能够帮你解决00_Rust安装及Hello World所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。