From adfe8b47b8aa62d91fc0d3622d44cb175041423e Mon Sep 17 00:00:00 2001 From: mcfd <43702470+mcfd@users.noreply.github.com> Date: Thu, 6 Apr 2023 15:21:34 +0800 Subject: [PATCH] =?UTF-8?q?Update=20and=20rename=20sources/tech/20230328.1?= =?UTF-8?q?=20=E2=AD=90=EF=B8=8F=E2=AD=90=EF=B8=8F=20Rust=20Basics=20Serie?= =?UTF-8?q?s=201=20Create=20and=20Run=20Your=20First=20Rust=20Program.md?= =?UTF-8?q?=20to=20translated/tech/20230328.1=20=E2=AD=90=EF=B8=8F?= =?UTF-8?q?=E2=AD=90=EF=B8=8F=20Rust=20Basics=20Series=201=20Create=20and?= =?UTF-8?q?=20Run=20Your=20First=20Rust=20Program.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...Series 1 Create and Run Your First Rust Program.md | 231 ----------------- ...Series 1 Create and Run Your First Rust Program.md | 232 ++++++++++++++++++ 2 files changed, 232 insertions(+), 231 deletions(-) delete mode 100644 sources/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md create mode 100644 translated/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md diff --git a/sources/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md b/sources/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md deleted file mode 100644 index e9c3fba36a..0000000000 --- a/sources/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md +++ /dev/null @@ -1,231 +0,0 @@ -[#]: subject: "Rust Basics Series #1: Create and Run Your First Rust Program" -[#]: via: "https://itsfoss.com/rust-introduction/" -[#]: author: "Pratham Patel https://itsfoss.com/author/pratham/" -[#]: collector: "lkxed" -[#]: translator: " " -[#]: reviewer: " " -[#]: publisher: " " -[#]: url: " " - -Rust Basics Series #1: Create and Run Your First Rust Program -====== - -![][1] - -The Rust programming language is one of the fastest adopted systems programming languages by developers and tech companies. It is also voted as one of the **most loved programming languages** by developers who use it on a daily basis. Rust has [been getting this love for][2]**[seven consecutive years][2]** now! - -It is so popular that there are now two big efforts being carried out in the Linux ecosystem: - -- Inclusion of [Rust as a secondary programming language in the Linux kernel][3] -- System76 is [writing their own desktop environment from scratch using Rust][4] - -And that is just in the Linux ecosystem. Android's Bluetooth implementation [Gabeldorsche][5] is now written in Rust. - -Do you see the rising popularity of Rust? You would probably like to learn coding in Rust. - -### Why should you consider Rust over other programming languages? - -Rust is a programming language that has an **extremely strict type system**. As a result, you are "forced" to not write bad code in the first place (well, usually). - -The Rust programming language has the following "goals": - -- **Speed**: Rust's binaries are as fast as C binaries, sometimes outpacing C++ binaries! -- **Memory safety**: Rust has a huge emphasis on memory safety. -- **Concurrency**: Focusing on memory safety eliminates a lot of race condition-like scenarios and helps you introduce concurrency in your program. - -Following are a few errors mistakes one might make in languages like C/C++ (but not with Rust): - -- Use after free -- Double free -- Accessing out-of-bound values -- Using `NULL` -- Inappropriate pointer arithmetic and/or access -- Use of uninitialized variable(s) -- Thread-unsafe multi-threading - -Have a look at the issues caused by such issues at major corporations like [Apple][6], [Microsoft][7], [Google][8], [0day][9] etc, - -Now that you know why one might want to choose the Rust programming language over any other one, let's start with the Rust language tutorial series! - -### Intended audience - -For the love of Rust, I am writing this series of Rust tutorials to help you get acquainted with the concept of Rust programming. - -This tutorial series is intended for folks already familiar with programming languages like C and C++. I assume you know basic terms like _variables_, _functions_, _loops_, etc. - -The only prerequisites that I ask from you are your time and some effort. - -### Installing the Rust compiler - -I would prefer that you have the [Rust compiler installed locally][10]. You can do so by running the following command: - -``` -curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh -``` - -![Installing Rust on Ubuntu Linux][11] - -Apart from the Rust Compiler, I also recommend installing a few more tools that will help you in the development process: - -``` -rustup component add rust-src rust-analyzer rust-analysis -``` - -💡 - -If you do not wish to install the Rust compiler, no worries. You can run the Rust code directly in your browser! Just head over to the - -[Rust Playgrounds website][12] - - and paste the code discussed here. - -### Hello Rust! - -Since Dennis Ritchie and Brian Kernighan introduced the C programming language with the "Hello world" program, it has become a custom in the UNIX world to do so with any new programming language you learn. - -So let's write our Hello World program in Rust as well. - -I will [create a project directory][13] called `learn-rust-its-foss` in my home directory. In there, I create another directory called `hello-world`. Inside that, I will create a `main.rs` file: - -``` -// this code outputs the text -// "Hello world!" to `stdout` - -fn main() { - println!("Hello world!"); -} -``` - -📋 - -Just like C, C++ and Java source files have the extensions - -``` -.c -``` - -, - -``` -.cpp -``` - - and - -``` -.java -``` - - respectively, the Rust source files have the - -``` -.rs -``` - - file extension. - -As a C/C++ programmer, you might have used [gcc on Linux][14], `clang` on macOS and MSVC on Windows. But to compile Rust code, the language creators themselves provide an official `rustc` compiler. - -Running a Rust program is the same as [executing C/C++ program][15]. You compile the code to get the executable file and then run this executable to run the code. - -``` -$ ls -main.rs - -$ rustc main.rs - -$ ls -main main.rs - -$ ./main -Hello world! -``` - -Nice! - -### Deciphering Rust code - -Now that you wrote, compiled and ran your first ever Rust program, let's de-structure the "Hello world" code and understand each part. - -``` -fn main() { -} -``` - -The `fn` keyword is used to declare a function in Rust. Following it, `main` is the name of this particular function that was declared. Like many compiled programming languages, the `main` is a special function used as your program's entry point. - -Any code written inside the `main` function (between the curly brackets `{``}`) gets executed upon program start-up. - -#### println macro - -Inside the `main` function, there is one statement: - -``` -println!("Hello world!"); -``` - -Like the C language's standard library has the `printf` function, Rust language's standard library has the `println`**macro**. A macro is similar to a function but it is distinguished by the **exclamation mark**. You'll learn about macros and functions later in this series. - -The `println` macro takes in a format string and puts it to the program's output (in our case, that is terminal). Since I wish to output some text instead of a variable, I will enclose the text inside double quotes (`"`). Finally, I end this statement using a semi-colon to denote the end of the statement. - -📋 - -Just know that anything that looks like a function call but has an exclamation mark ( - -``` -! -``` - -) before the opening parentheses is a macro in the Rust programming language. - -#### Comments - -Rust follows the known commenting style of the C programming language. A single line comment starts with two forward slashes (`//`) and a multi-line comment is started by `/*` and ends with `*/`. - -``` -// this is a multi-line comment -// but nothing stops me to doing the same -// on the second or third line too! - -/* - * this is a "true" mutli-line comment - * because it is _fancy_ - */ -``` - -### Conclusion - -You just took the first step towards coding in Rust with the Hello World program. - -As a practice, perhaps you can write and execute a Rust program that prints "Yes! I did Rust". - -In the next part of the series, you'll learn to use variables in your Rust program. Stay tuned! - --------------------------------------------------------------------------------- - -via: https://itsfoss.com/rust-introduction/ - -作者:[Pratham Patel][a] -选题:[lkxed][b] -译者:[译者ID](https://github.com/译者ID) -校对:[校对者ID](https://github.com/校对者ID) - -本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 - -[a]: https://itsfoss.com/author/pratham/ -[b]: https://github.com/lkxed/ -[1]: https://itsfoss.com/content/images/2023/03/linux-mega-packt.webp -[2]: https://survey.stackoverflow.co/2022/?ref=itsfoss.com#section-most-loved-dreaded-and-wanted-programming-scripting-and-markup-languages -[3]: https://news.itsfoss.com/linux-kernel-6-1-release/?ref=itsfoss.com -[4]: https://news.itsfoss.com/pop-os-cosmic-rust/?ref=itsfoss.com -[5]: https://android.googlesource.com/platform//system/bt/+/83498aa554aea220fcff30b6310a0a7b4557969f/gd/rust/linux/stack/src/bluetooth.rs?ref=itsfoss.com -[6]: https://langui.sh/2019/07/23/apple-memory-safety/?ref=itsfoss.com -[7]: https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-systems-programming-language/?ref=itsfoss.com -[8]: https://security.googleblog.com/2019/05/queue-hardening-enhancements.html?ref=itsfoss.com -[9]: https://docs.google.com/spreadsheets/d/1lkNJ0uQwbeC1ZTRrxdtuPLCIl7mlUreoKfSIgajnSyY/view?ref=itsfoss.com#gid=1190662839 -[10]: https://itsfoss.com/install-rust-cargo-ubuntu-linux/ -[11]: https://itsfoss.com/content/images/2023/03/install-rust.svg -[12]: https://play.rust-lang.org/?ref=itsfoss.com -[13]: https://itsfoss.com/make-directories/ -[14]: https://learnubuntu.com/install-gcc/?ref=itsfoss.com -[15]: https://itsfoss.com/run-c-program-linux/ \ No newline at end of file diff --git a/translated/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md b/translated/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md new file mode 100644 index 0000000000..8467bdb2b6 --- /dev/null +++ b/translated/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md @@ -0,0 +1,232 @@ +[#]: subject: "Rust Basics Series #1: Create and Run Your First Rust Program" +[#]: via: "https://itsfoss.com/rust-introduction/" +[#]: author: "Pratham Patel https://itsfoss.com/author/pratham/" +[#]: collector: "lkxed" +[#]: translator: "mcfd" +[#]: reviewer: " " +[#]: publisher: " " +[#]: url: " " + +Rust 基础系列 #1: 创建并运行你的首个 Rust 程序 +====== + +![][1] + +Rust 是被开发者和科技公司所采用最快的系统编程语言之一。它也被每天使用它的开发者评为**最受欢迎的编程语言**之一。其[已经][2]**[连续七年][2]**[受到这种欢迎了][2]! + +它是如此的受欢迎,以致于现在有两股巨大的推力将其带入 Linux 生态系统中: + +- 包括 [Rust 将作为 Linux 内核的第二支持语言][3] +- System76 [正在使用 Rust 重写他们自己的桌面环境][4] + +而这仅仅是在 Linux 生态系统中。安卓的蓝牙实现 [Gabeldorsche][5] 现在也是由 Rust 编写的。 + +你是否看到了 Rust 的流行趋势?你大概率可能想学习使用 Rust 进行编程。 + +### 为什么你要考虑 Rust 而不是其他编程语言? + +首先,Rust 是一门 **类型安全的编程语言** 并且 **拥有极其严格的编译期检查**。因此,你首先会 “被迫” 写不出不安全的代码(好吧,通常)。 + +Rust 编程语言有以下 "目标": + +- **性能**:Rust 的二进制文件和 C 语言的二进制文件一样快,有时甚至超过了 C++ 的二进制文件! +- **内存安全**:Rust 非常重视内存安全。 +- **并发**:对内存安全的关注消除了很多类似竞争的情况,并帮助你在程序中无畏并发。 + +以下是在 C/C++ 等语言中可能犯的一些错误(但不是 Rust): + +- 释放后使用 +- 双重释放 +- 越界访问 +- 使用 `NULL` +- 不适当的指针运算或访问 +- 使用未经初始化的变量 +- 线程不安全的多线程 + +看看 [Apple][6], [Microsoft][7], [Google][8], [0day][9] 等大公司因这类错误而引起的问题吧。 + +现在你可能知道了为什么要选择 Rust 语言而不是其他编程语言,让我们开始学习 Rust 语言的系列教程吧! + +### 目标受众 + +出于对 Rust 的热爱,我写了这个系列的 Rust 教程,帮助你熟悉 Rust 编程的概念。 + +这个教程系列是为已经熟悉 C 和 C++ 等编程语言的人准备的。我假设你已经知道了 _变量_、_函数_、_循环_ 等基本术语。 + +我对你的唯一要求是你不懈的坚持与努力。 + +### 安装 Rust 工具链 + +我希望你能在本地安装 [Rust 工具链][10]。你可以通过运行以下命令来做到这一点: +(LCTT 译注:如果你使用 Linux 发行版,请不要直接安装软件源里的 Rust 工具链,尽管这样看起来很便捷) + +``` +curl --proto '=https' --tlsv1.3 -sSf https://sh.rustup.rs | sh +``` + +![Installing Rust on Ubuntu Linux][11] + +除了 Rust 基本工具链,我还建议再安装一些工具,这些工具将在开发过程中帮助你: + +``` +rustup component add rust-src rust-analyzer rust-analysis +``` + +💡 + +如果你不希望在本地安装 Rust 工具链,不用担心。你还可以直接在你的浏览器中运行 Rust 代码!只要到 + +[Rust Playgrounds website][12] + + 并把所讨论的代码粘贴在此处。 + +### Hello Rust! + +自从 Dennis Ritchie 和 Brian Kernighan 用 "Hello world" 程序介绍了 C 语言后,在 UNIX 世界里,对你学习的任何新的编程语言都要这样做,这已经成为一种习惯。 + +因此,让我们也用 Rust 编写我们的 Hello World 程序。 + +我将在我的家目录里[新建一个项目目录][13]叫做 `learn-rust-its-foss`。然后,在这里我将新建一个叫 `hello-world` 的目录。最后,在里面新建 `main.rs` 文件: + +``` +// 这串代码将打印字符 +// "Hello world!" 将被打印到 `标准输出` + +fn main() { + println!("Hello world!"); +} +``` + +📋 + +就像 C、C++ 和 Java 源代码文件的扩展名是 + +``` +.c +``` + +、 + +``` +.cpp +``` + + 和 + +``` +.java +``` + + Rust 的源文件扩展名是 + +``` +.rs +``` + + 文件扩展名 + +作为一个 C/C++ 程序员,你可能已经在 [Linux 上使用 GCC][14],在 macOS 上使用 `Clang`,在 Windows 上使用 MSVC。但是为了编译 Rust 代码,该语言的创造者自己提供了一个官方的 `rustc` 编译器。 + +运行 Rust 程序和[执行 C/C++ 程序][15]是一样的。你首先编译代码,然后得到可执行文件,最后再运行这个可执行文件从而来运行代码。 + +``` +$ ls +main.rs + +$ rustc main.rs + +$ ls +main main.rs + +$ ./main +Hello world! +``` + +很好! + +### 破译 Rust 代码 + +现在你已经编写、编译并运行了你的第一个 Rust 程序,让我们对 "Hello world" 的代码进行解构并理解每一部分。 + +``` +fn main() { +} +``` + +`fn` 关键字是用来在 Rust 中声明一个函数的。在它后面 `main` 是这个被声明函数的名字。像许多编译型编程语言一样,`main` 是一个特殊的函数,用来作为你的程序的入口。 + +任何写在 `main` 函数里的代码(在大括号 `{``}` 中)将在程序被启动时运行。 + +#### println 宏 + +在 `main` 函数中, 有一个语句(LCTT 译注:语句 区别于 表达式): + +``` +println!("Hello world!"); +``` + +就像C语言的标准库有 `printf` 函数一样,Rust语言的标准库有 `println` **macro**。宏类似于函数,但它以**感叹号**来区分。你将在本系列的后面学习宏和函数的知识。 + +`println` 宏接收一个格式化的字符串,并把它放到程序的标准输出中(在我们的例子中,就是终端)。由于我希望输出一些文本而不是一个变量,我将把文本放在双引号(`"`)内。最后,我用一个分号来结束这个语句,表示语句的结束。 + +📋 + +你只需知道,任何看起来像函数调用但有感叹号的东西 + +``` +! +``` + +就是 Rust 编程语言中的一个宏。 + +#### 注释 + +Rust 遵循已知的 C 编程语言的注释风格。单行注释以两个正斜杠(`//`)开始,多行注释以 `/*` 开始,以 `*/` 结束。 + +``` +// this is a multi-line comment +// but nothing stops me to doing the same +// on the second or third line too! + +/* + * this is a "true" mutli-line comment + * because it is _fancy_ + */ +``` + +### 总结 + +你刚刚通过 Hello World 程序迈出了用 Rust 写代码的第一步。 + +作为一种练习,也许你可以编写并执行一个打印出 "Yes! I did Rust" 的 Rust 程序。 + +在本系列的下一部分中,你将学习在 Rust 程序中使用变量。敬请期待! + +-------------------------------------------------------------------------------- + +via: https://itsfoss.com/rust-introduction/ + +作者:[Pratham Patel][a] +选题:[lkxed][b] +译者:[mcfd](https://github.com/mcfd) +校对:[校对者ID](https://github.com/校对者ID) + +本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出 + +[a]: https://itsfoss.com/author/pratham/ +[b]: https://github.com/lkxed/ +[1]: https://itsfoss.com/content/images/2023/03/linux-mega-packt.webp +[2]: https://survey.stackoverflow.co/2022/?ref=itsfoss.com#section-most-loved-dreaded-and-wanted-programming-scripting-and-markup-languages +[3]: https://news.itsfoss.com/linux-kernel-6-1-release/?ref=itsfoss.com +[4]: https://news.itsfoss.com/pop-os-cosmic-rust/?ref=itsfoss.com +[5]: https://android.googlesource.com/platform//system/bt/+/83498aa554aea220fcff30b6310a0a7b4557969f/gd/rust/linux/stack/src/bluetooth.rs?ref=itsfoss.com +[6]: https://langui.sh/2019/07/23/apple-memory-safety/?ref=itsfoss.com +[7]: https://msrc-blog.microsoft.com/2019/07/18/we-need-a-safer-systems-programming-language/?ref=itsfoss.com +[8]: https://security.googleblog.com/2019/05/queue-hardening-enhancements.html?ref=itsfoss.com +[9]: https://docs.google.com/spreadsheets/d/1lkNJ0uQwbeC1ZTRrxdtuPLCIl7mlUreoKfSIgajnSyY/view?ref=itsfoss.com#gid=1190662839 +[10]: https://itsfoss.com/install-rust-cargo-ubuntu-linux/ +[11]: https://itsfoss.com/content/images/2023/03/install-rust.svg +[12]: https://play.rust-lang.org/?ref=itsfoss.com +[13]: https://itsfoss.com/make-directories/ +[14]: https://learnubuntu.com/install-gcc/?ref=itsfoss.com +[15]: https://itsfoss.com/run-c-program-linux/