Rust安装与编写第一个rust程序

2023-05-16

Rust 是 Mozilla 开发的注重安全、性能和并发性的编程语言。

下边来演示一下如何安装rust,并尝试创建第一个rust项目。

使用 rustup 脚本安装:

第一步: 执行 curl https://sh.rustup.rs -sSf | sh, 下载安装器:

$ 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:
  /Users/wangtom/.cargo/bin

This path will then be added to your PATH environment variable by modifying the
profile files located at:
  /Users/wangtom/.profile
  /Users/wangtom/.zprofile
  /Users/wangtom/.bash_profile

You can uninstall at any time with rustup self uninstall and these changes will
be reverted.

第二步,命令行会提示安装选项,选择一个(1)默认安装选项:

Current installation options:
   default host triple: x86_64-apple-darwin
   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-apple-darwin'
info: latest update on 2018-12-06, rust version 1.31.0 (abe02cefd 2018-12-04)
info: downloading component 'rustc'
 62.6 MiB /  62.6 MiB (100 %) 617.6 KiB/s ETA:   0 s
info: downloading component 'rust-std'
 49.0 MiB /  49.0 MiB (100 %) 682.7 KiB/s ETA:   0 s
info: downloading component 'cargo'
  3.6 MiB /   3.6 MiB (100 %) 609.3 KiB/s ETA:   0 s
info: downloading component 'rust-docs'
  8.5 MiB /   8.5 MiB (100 %) 559.0 KiB/s ETA:   0 s
info: installing component 'rustc'
info: installing component 'rust-std'
info: installing component 'cargo'
info: installing component 'rust-docs'
info: default toolchain set to 'stable'

  stable installed - rustc 1.31.0 (abe02cefd 2018-12-04)

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

如果屏幕上出现 Rust is installed now. Great!等字样, 就说明安装成功了。

尝试运行一下 cargo 等命令,却报命令不存在。可以看到,刚才上边的安装提示中,已经提示了PATH环境变量已经添加到.profile等文件了,为什么命令不存在呢?

$ which cargo
cargo not found
$ which rustc
rustc not found

查看 ~/.cargo/bin 目录,各个命令已经存在了:

$ ls -l ~/.cargo/bin 
total 132560
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo-clippy
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 cargo-fmt
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rls
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rust-gdb
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rust-lldb
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustc
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustdoc
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustfmt
-rwxr-xr-x  10 wangtom  staff  6786016 12 11 17:03 rustup

查看 ~/.bash_profile 文件,已经存在了命令导出:

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

重新加载一下 .bash_profile 配置即可:

$ source ~/.bash_profile

再次使用 cargorustc 等命令查看,已经可以正常找到路径了:

$ which cargo
/Users/wangtom/.cargo/bin/cargo
$ which rustc
/Users/wangtom/.cargo/bin/rustc

$ cargo --version
cargo 1.31.0 (339d9f9c8 2018-11-16)
$ rustc --version
rustc 1.31.0 (abe02cefd 2018-12-04)

可以看到,目前安装的是 rust 1.31.0 版本。

来一个HelloWorld程序

现在,已经安装好了 Rust,接下来让我们开始写第一个 Rust 程序。
按照惯例,当然是HelloWorld了。

Cargo, 是rust的构建工具,同时也是rust的包管理器。

使用 cargo new 命令创建一个项目,名为hello_world:

$ cargo new hello_world
     Created binary (application) `hello_world` package

直接进入 hello_world 目录,查看一下命令结果,生成了一个 Cargo.toml 文件和一个 main.rs 文件:

$ cd hello_world
$ tree .
.
├── Cargo.toml
└── src
    └── main.rs

1 directory, 2 files

查看一下 Cargo.toml 文件:

[package]
name = "hello_world"
version = "0.1.0"
authors = ["wangtom"]
edition = "2018"

[dependencies]

打开 src/main.rs 文件, 其代码如下:

fn main() {
    println!("Hello, world!");
}

构建代码:

$ cargo build   
 Compiling hello_world v0.1.0 (/Users/wangtom/Code/rust/hello_world)
  Finished dev [unoptimized + debuginfo] target(s) in 2.53s 

执行构建后,会生成一个 target/debug 目录,里边有一堆文件,同时在项目根目录也生成了一个Cargo.lock文件,看起来和 node 的 package-lock.json 或者PHP的 composer.lock 类似。

运行构建结果:

$ ./target/debug/hello_world
Hello, world!

也可以直接使用 cargo run 命令,直接构建并执行构建结果:

$ cargo run
 Finished dev [unoptimized + debuginfo] target(s) in 0.08s 
  Running `target/debug/hello_world`
Hello, world!

使用 rustc 运行

创建一个 hello_world2 目录,并创建一个 main.rs:

$ mkdir hello_world2
$ cd hello_world2 
$ vi main.rs

main.rs 文件中,写下 Hello, world 程序:

fn main() {
    println!("Hello, world!");
}

使用 rustc 命令编译,然后可以直接执行编译结果,依然可以正常输出。

$ rustc main.rs 
$ ll
total 1152
drwxr-xr-x  4 wangtom  staff     128 12 11 18:10 ./
drwxr-xr-x  4 wangtom  staff     128 12 11 18:09 ../
-rwxr-xr-x  1 wangtom  staff  584444 12 11 18:10 main*
-rw-r--r--  1 wangtom  staff      46 12 11 18:10 main.rs
$./main 
Hello, world!

这个rustc命令看起来和Java里的javac命令作用一样。

参考链接

https://doc.rust-lang.org/cargo/guide/index.html

[END]

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Rust安装与编写第一个rust程序 的相关文章

随机推荐

  • SQL数据分析之子查询的综合用法和案例题【耐心整理】

    文章目录 零 写在前面一 子查询基础用法二 子查询综合用法 xff08 难题 xff09 零 写在前面 本文所有代码均是在SQL ZOO平台进行 xff0c 数据也该平台下的world表和一些其他平台提供的数据表 xff0c 所有代码均已通
  • python变量命名规范

    一 变量的命名 变量是对象的引用 xff0c 变量存放于栈中 xff0c 对象存放于堆中 python对变量的命名比较简明 xff0c 不得使用数字开头即可 xff0c 在此可引申下 xff0c 包和变量名 xff0c 全都小写 xff0c
  • python-基本数据运算

    一 基本数据运算 xff1b 0不能作除数 xff0c 否则报错 1 43 加 1 43 2 61 3 2 减 3 1 61 2 3 乘 3 2 61 6 4 除 6 2 61 3 5 整除 7 2 61 3 6 求余数 9 2 61 1
  • Verilog电路设计小技巧之表达式位宽

    不积跬步无以至千里 记录Verilog电路设计中的点点滴滴 今天想说说verilog中表达式的位宽问题 xff0c 编码过程中 xff0c 经常会出现很多表达式位宽不匹配 基本上在跑lint的时候 xff0c 只要一个表达式中有任意2个操作
  • MicroPython在Pycharm上的运行方式

    安装插件micropython 安装CP210x VCP Winodws 可在设备管理器中查看 安装python环境变量envs 在Pycharm中激活环境变量 eg xff1a activate MicroPython pip insta
  • VS2012运行C++程序出现fatal error LNK1104: 无法打开文件“kernel32.lib”错误

    VS2012运行C 43 43 程序出现fatal error LNK1104 无法打开文件 kernel32 lib 错误 一 本人的安装环境 xff1a win7 64位 二 解决方法 当时安装完成之后 xff0c 运行一个简单的Hel
  • 调试llvm时出现collect2: fatal error: ld terminated with signal 9

    安装llvm debug版本 release版本的安装过程可以参考https blog csdn net vincentuva article details 82993563 在安装使用debug版本时 xff0c 只需要进入到build
  • 地址栏中的#是什么意思

    我们在开发vue时 xff0c 地址栏中会出现 xff0c 如下图所示 xff1a 当我们点击跳转A页面时 xff1a 那么地址栏中的 到底是什么意思 xff1f 如何去掉 xff1f 路由的两种显示模式 Hash模式 这个模式下地址栏中包
  • Anaconda静默安装

    我们默认安装Anaconda时 xff0c 需要进行交互才可以完成安装 xff0c 例如需要输入yes xff0c 或者回车 有时候我们只需要他进行默认安装即可 xff0c 不需要进行交互 命令如下 xff1a span class tok
  • 使用godoc创建可以本地浏览的go文档站点

    Golang 的官网地址是golang org xff0c 有的时候国内打不开 现在国内的可以使用 xff08 golang google cn xff09 其实 xff0c 如果已经安装好了 go xff0c 可以在本地直接查看go 文档
  • 慎用 dpkg --force-all 安装 linuxqq

    最近与一个朋友联络 xff0c 要用 QQ 去 xff31 xff31 官网上只有比较早的版本 xff0c 安装后 xff0c 还提示必须更新 xff0c 点击还找不到下载 xff0c 悲了个催 后面幸好有 web qq 暂时解决了这个问题
  • 又开了一个BLOG,发个贴纪念一下

    很久没写技术方面的BLOG了 xff0c 最近因为查询资料看了很多比人的BLOG 感觉心里有些痒痒的 xff0c 所以决定再次开一个BLOG xff0c 本人是搞嵌入式开发的 xff0c 对各种通讯终端 xff0c CPU以及LINUX比较
  • How to make linux boot from network

    1 Enable dhcp server on a linux server the etc dhcpd conf should be looked like this ddns update style interim ignore cl
  • 转回CSDN了

    在BLOGSPOT上面挣扎了几个月 xff0c 还是放弃了 xff0c 毕竟看我的BLOG绝大多数肯定是中国人 xff0c 一个老被中国政府封的BLOG是没办法让别人接受的 xff0c 哎 xff0c 还是对BLOGSPOT恋恋不舍阿 xf
  • 最近在研究Mythtv

    mythtv是一个linux下的开源电视节目观看 录制和管理的软件 xff0c 如果装在普通PC上就可以变成一台电视PC xff0c 可以使用遥控器来看电视 xff0c 录节目 xff0c 如果装在一个嵌入式系统里就是一个机顶盒啦 xff0
  • arm linux 内核移植及驱动调试-网卡(1)

    最近在给一块ARM开发板 扬创的2440 移植新的kernel xff0c 原来的驱动都在 xff0c 不过还是碰到不少问题 xff0c 主要是对ARM LINUX的一些结构不甚了解 xff0c 这里作个笔记以便自己或他人查阅 前面没有什么
  • UBUNTU 下的IE6搞定

    可以上网上银行 支付宝 就是有点卡 不知道为什么 另外还有些小bug 不过我已经非常满意了 先装好wine 在ubuntu下面就是 sudo apt get install wine cabextract 然后直接下载安装ie4linux就
  • UBUNTU 下编译POKY

    记录一下我在UBUNTU下编译POKY的一些TIPS xff0c 防止以后再编的时候忘掉 xff0c 又要重新GOOGLE 1 解开pinky的包 2 进入pinky目录输入 source poky init build env 3 修改b
  • LLVM编译collect2: fatal error: ld terminated with signal 9

    报错 xff1a collect2 fatal error ld terminated with signal 9 查了一下这个报错 xff0c 可能是内存不足 xff0c 看到有前人的解决方法 xff0c 创建了20G的交换空间 xff1
  • Rust安装与编写第一个rust程序

    Rust 是 Mozilla 开发的注重安全 性能和并发性的编程语言 下边来演示一下如何安装rust xff0c 并尝试创建第一个rust项目 使用 rustup 脚本安装 xff1a 第一步 xff1a 执行 curl https sh