Hello, Rust 🤝

简介: Hello, Rust 🤝

前言

一个做过点后端运维的前端攻城狮,热爱开源,喜欢折腾,为什么选择进入 Rust 领域,首先是被它的高性能,高安全性,应用范围之广等特性所吸引,同时 Rust 也是前端基建中的一员,GitHub 使用 Rust 重写了搜索引擎,字节使用 Rust 开源了 Rspack 来提升大型的项目性能......

总之,这么优秀的语言,值得深度去体验和使用

安装 Rust

根据官网的主页文档步骤进行安装,通过 RUSTUP-INIT.EXE 安装,在 Win11 系统安装成功后,使用命令检查版本,1.74.1 最新版本

$ cargo -V
cargo 1.74.1 (ecb9851af 2023-10-18)

创建项目

使用 cargo new hello 创建一个新的 hello world Rust 程序

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

这时候会创建一个 hello 的目录,里面 src 为代码工程目录,平级的是一些配置文件,分别是 Cargo.lockCargo.toml.gitignore

运行项目

使用 cargo run 运行项目,发现不管运行 cargo run 命令还是运行 cargo build 命令都会报如下错误

PS D:\xxx\hello> cargo build
   Compiling hello v0.1.0 (D:\CodeGitHub\Rust\hello)
error: linker `link.exe` not found
  |
  = note: program not found
note: the msvc targets depend on the msvc linker but `link.exe` was not found
note: please ensure that Visual Studio 2017 or later, or Build Tools for Visual Studio were installed with the Visual C++ option.
note: VS Code is a different product, and is not sufficient.
error: could not compile `hello` (bin "hello") due to previous error

提示 请确保使用Visual C++选项安装了Visual Studio 2017或更高版本,或Visual Studio的Build Tools,查了一下本地软件,之前安装了 Visual Studio 2022,看来是没有安装 Visual C++ 选项,由于不用 Visual Studio 2022,我选择直接卸载了,只安装 Visual Studio 生成工具

Microsoft C++ 生成工具

Visual Studio Installer 工具安装好的效果

image.png

注意! 这个是只安装生成工具,不是安装整个 Visual Studio IDE

安装成功后,Visual Studio 2022 的目录

image.png

再次执行 cargo run

如果 Visual Studio 生成工具安装成功的话会出现如下的打印输出效果

$ cargo run
   Compiling hello v0.1.0 (D:\CodeGitHub\Rust\hello)
    Finished dev [unoptimized + debuginfo] target(s) in 0.86s
     Running `target\debug\hello.exe`
Hello, world!

如果认为 Visual Studio 太占空间,可考虑下面方式

因为 Visual Studio 生成工具占用磁盘空间较大,也可以使用下面的方式

windows下运行rust报错error: linker link.exe not found

使用 rustup 命令安装 stable-x86_64-pc-windows-gnu

rustup toolchain install stable-x86_64-pc-windows-gnu

提示!

我尝试在本地执行命令的方式安装,发现在命令行下安装下载的速度慢如蜗牛,公司的网络下载 Visual Studio 工具速度每秒几兆

image.png

再执行如下命令

rustup default stable-x86_64-pc-windows-gnu

这时候也是可以正常运行项目的

目录分析对比

cargo 创建的项目工程目录结构,和前端 vue-cli 之类的脚手架创建出来的目录结构类似,核心代码文件都在 src 目录下

image.png

Cargo.lock 文件类似前端项目中的 package-lock.jsonyarn.lock

Cargo.toml 文件类似前端项目中的 package.json 存项目依赖包文件对应的版本配置

项目编译文件在 target 目录中,类似前端项目中的 dist 目录,target 目录中里面有编译好的 hello.exe 文件可以直接运行,使用 cargo clean 可以清理 target 目录

main.rs 是整个工程的入口文件,里面的 main() 是主函数,cargo run 命令执行后会直接运行主函数

image.png

main.rs 里面的内容还是比较简洁的,只有一个 main() 函数就能直接运行了

小结一下

安装的过程很顺利,运行后报错的情况需要手动处理,这一点上感觉没有 Node 那种一次安装就能直接使用的顺畅感觉,整体目录结构文件和当前主流框架的脚手架创建的前端目录类似,标准的工程化目录结构没有什么生疏的感觉,包括 src 里面的入口文件和里面的 main() 函数等,包括 fn 的声明函数的方式,和前端中的 function 缩写一致,目前上手的内容感觉还是比较简单的,整个工程方面的也是充满了相似的感觉,后期会进一步深度学习Rust 中的语法,特性以及整个生态内容,也会定期分享...

相关文章
|
6天前
|
Rust 安全 开发者
Rust
Rust
30 0
|
6天前
|
Rust Java C语言
【一起学Rust】Rust的Hello Rust详细解析
【一起学Rust】Rust的Hello Rust详细解析
125 0
|
6天前
|
Rust JavaScript 前端开发
【一起学Rust】Rust介绍与开发环境搭建
【一起学Rust】Rust介绍与开发环境搭建
143 0
|
Rust JavaScript 前端开发
【Rust 实战】Rust 与 Wasm
【Rust 实战】Rust 与 Wasm
1622 0
【Rust 实战】Rust 与 Wasm
|
6天前
|
Rust 安全 程序员
开发语言漫谈-rust
if(){}else{}就是C家族的
|
6天前
|
Rust 安全
|
6天前
|
Rust Linux 开发工具
Rust安装
该文介绍了如何在Linux、mac和Windows上安装Rust编程语言。在Linux和mac系统中,可以通过运行一个curl命令自动下载并安装;在Windows上,需从官方网站下载安装包。安装完成后,使用`rustc --version`检查Rust版本以确认安装成功。此外,还提到了如何更新Rust(使用`rustup update`)和卸载(使用`rustup self uninstall`)以及查看官方文档(运行`rustup doc`)。推荐的开发工具有Visual Studio Code和JetBrains CLion,需要安装Rust插件。
|
6天前
|
Rust 编译器 容器
|
6天前
|
Rust 算法 C++
【从 C++ 到 Rust:掌握 Rust 基础语法】From C++ to Rust: Mastering Rust Basics
【从 C++ 到 Rust:掌握 Rust 基础语法】From C++ to Rust: Mastering Rust Basics
48 0
|
6天前
|
Rust 编译器 Linux
【Rust】——rust前言与安装rust
【Rust】——rust前言与安装rust
35 0
【Rust】——rust前言与安装rust