Rust编程:零基础入门高性能开发想掌握一门兼具C++性能与现代安全性的编程语言?Rust正是你的理想选择!作为席卷系统编程和高性能开发领域的“新星”,Rust不仅被Firefox和GoogleFuschia等大厂采用,还因其零基础友好的学习路径受到开发者追捧。本文将带你从安装
想掌握一门兼具 C++ 性能与现代安全性的编程语言?Rust 正是你的理想选择!作为席卷系统编程和高性能开发领域的“新星”,Rust 不仅被 Firefox 和 Google Fuschia 等大厂采用,还因其零基础友好的学习路径受到开发者追捧。本文将带你从安装 Rust 到编写第一个程序,零基础也能快速上手,解锁高性能开发的无限可能!快来跟随这篇入门指南,开启你的 Rust 编程之旅吧!
本文全面介绍 Rust 编程语言的入门知识,涵盖其高性能、内存安全和并发处理的独特优势,以及与 C/C++、Java 等语言的对比。Rust 广泛应用于系统编程、WebAssembly 和高性能 Web 服务,深受 Firefox、Google Fuschia 等项目青睐。文章详细讲解了 Rust 的安装步骤、开发环境配置、第一个“Hello World”程序的编写,以及使用 Cargo 工具进行项目管理。尽管 Rust 学习曲线稍陡,但其强大的性能和安全性使其成为零基础开发者迈向高性能开发的理想起点。
官网:<https://www.rust-lang.org/zh-CN/learn/get-started>
Windows:按官网指示操作
Mac 安装:
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
rustup update
rustup self uninstall
rustc --version
rustup doc
可在浏览器打开本地文档➜ cargo --version
cargo 1.67.1 (8ecd4f20a 2023-01-10)
~
➜ rustc --version
rustc 1.67.1 (d5a82bbd2 2023-02-07)
~
➜ rustup doc
编写Rust程序
➜ mkdir rust
~
➜ cd rust
~/rust
➜ mkdir hello_world
~/rust
➜ cd hello_world
~/rust/hello_world
➜ code .
~/rust/hello_world
➜ pwd
/Users/qiaopengjun/rust/hello_world
~/rust/hello_world via 🦀 1.67.1
➜ mv hello_world.rs main.rs
~/rust/hello_world via 🦀 1.67.1
➜ rustc main.rs
~/rust/hello_world via 🦀 1.67.1
➜ ls
main main.rs
➜ ./main
Hello World!
main.rs`文件
fn main() {
println!("Hello World!");
}
fn main(){}
main
函数很特别:它是每个Rust可执行程序最先运行的代码printIn!("Hello, world!");
rustc
源文件名
rustc main.rs
.pdb
文件,里面包含调试信息~/rust/hello_world via 🦀 1.67.1
➜ cargo --version
cargo 1.67.1 (8ecd4f20a 2023-01-10)
cargo new hello_cargo
hello_cargo
hello_cargo
main.rs
.gitignore
~/rust
➜ cargo new hello_cargo
Created binary (application) `hello_cargo` package
~/rust
➜ ls
hello_cargo hello_world
~/rust
➜ cd hello_cargo
hello_cargo on master [?] via 🦀 1.67.1
➜ ls
Cargo.toml src
hello_cargo on master [?] via 🦀 1.67.1
➜ ➜ ls
Cargo.toml src
Cargo.toml
src/main.rs
./target/debug/hello_cargo
或.\target\debug\hello_cargo.exe(Windows)
cargo build
会在顶层目录生成cargo.lock文件
cargo run
hello_cargo on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜ cargo run
Compiling hello_cargo v0.1.0 (/Users/qiaopengjun/rust/hello_cargo)
Finished dev [unoptimized + debuginfo] target(s) in 0.38s
Running `target/debug/hello_cargo`
Hello, world!
hello_cargo on master [?] is 📦 0.1.0 via 🦀 1.67.1
➜
cargo check
尽量用Cargo
Rust 编程语言以其卓越的性能、内存安全性和现代并发能力,正在成为高性能开发领域的首选。从 Firefox 的 CSS 渲染引擎到 Google Fuschia 操作系统,Rust 的实际应用证明了其强大潜力。本文通过安装配置、Hello World 程序和 Cargo 工具的介绍,助你零基础迈出 Rust 学习第一步。尽管 Rust 的独特概念需要时间掌握,但其简洁的语法和强大的生态系统让每位开发者都能轻松上手。立即安装 Rust,编写你的第一个高性能程序,探索编程的未来!
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!