欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 手游 > 1小时放弃Rust(1): Hello-World

1小时放弃Rust(1): Hello-World

2025/2/22 1:22:38 来源:https://blog.csdn.net/baiyu33/article/details/144598706  浏览:    关键词:1小时放弃Rust(1): Hello-World

1. 目的

最近被公司项目的 crash 问题折磨, 一方面是crash位置不固定,难以复现;另一方面是代码、sdk、人员众多,我拿不到所有代码。排查效率非常低效,各种会议满天飞,问题实质毫无进展。希望在达到被辞退的节点前学会 Rust,写一些不可能存在内存踩踏问题的 sdk,拯救自己。

2. 安装 Rust

https://www.rust-lang.org/learn/get-started

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

3. 更新 Rust

rustup update

4. 创建 Hello-World 工程

mkdir -p ~/play/rust
cd $_
cargo new hello-world

5. 编译和运行

cd hello-world
cargo build
cargo run

6. 目录结构

刚执行 cargo new hello-world 时,文件和目录结构为:

➜  hello-world git:(main) ✗ tree -L 3
.
├── Cargo.lock
├── Cargo.toml
└── src└── main.rs2 directories, 3 files

也可以 cargo clean 来恢复。

7. main.rs

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

解释:

  • fn: Rust 语言中的关键字。function 的意思
  • main(): 入口函数
  • println!: 宏. 暂时不理解,当做函数使用;会自动添加换行符的打印.

8. Cargo.toml

[package]
name = "hello-world"
version = "0.1.0"
edition = "2021"[dependencies]

完全看不懂。 忽略。

9. Cargo run 在运行什么

cargo run
./target/debug/hello-world

10. 不用 cargo 呢?

hello.rs:

fn main()
{println!("Hello, Rust");println!("What a day!");
}

编译:

rustc hello.rs

运行:

./hello

结果:

Hello, Rust
What a day!

11. 总结

给出了 Rust 的安装,以及创建、运行、简单理解 hello-world 工程的步骤。

12. 参考

  • https://www.rust-lang.org/learn/get-started
  • https://blog.csdn.net/weixin_50964512/article/details/130138973

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

热搜词