From c30169674979f4fb159594f61d5b4b349e1bb05f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=85=AD=E5=BC=80=E7=AE=B1?= Date: Tue, 28 Mar 2023 19:04:16 +0800 Subject: [PATCH] =?UTF-8?q?[=E6=89=8B=E5=8A=A8=E9=80=89=E9=A2=98][tech]:?= =?UTF-8?q?=2020230328.1=20=E2=AD=90=EF=B8=8F=E2=AD=90=EF=B8=8F=20Rust=20B?= =?UTF-8?q?asics=20Series=201=20Create=20and=20Run=20Your=20First=20Rust?= =?UTF-8?q?=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 ++++++++++++++++++ 1 file changed, 231 insertions(+) create mode 100644 sources/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 new file mode 100644 index 0000000000..e9c3fba36a --- /dev/null +++ b/sources/tech/20230328.1 ⭐️⭐️ Rust Basics Series 1 Create and Run Your First Rust Program.md @@ -0,0 +1,231 @@ +[#]: 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