当 Rust 遇上 Fedora
Rust
![](/data/attachment/album/201610/20/084534czc55b701z8g36cl.jpg)


### Rust 是什么？


Rust 是一种系统编程语言，它运行速度惊人，并且可以避免几乎所有的崩溃、[内存区块错误](https://wikipedia.org/wiki/Segmentation_fault) 以及数据竞争。你也许会质疑为什么我们还需要又一种这样的语言，因为已经有很多同类的语言了。这篇文章将会告诉你为什么。


### 安全性 vs. 控制权


![](/data/attachment/album/201610/20/084548zqum564qt5848zkz.png)


你也许见过上面的图谱。一边是 C/C++，对运行的硬件拥有更多的控制权，因此它可以让开发者通过对所生成的机器代码进行更精细的控制来优化性能。然而这不是很安全，这很容易造成内存区块错误以及像 [心血漏洞](https://fedoramagazine.org/update-on-cve-2014-0160-aka-heartbleed/) 这样的安全漏洞。


另一边是像 Python、Ruby 和 JavaScript 这种没有给予开发者多少控制权但是可以创建出更安全的代码的语言。虽然这些代码可以生成相当安全并且可控的异常，但是它们不会造成内存区块错误。


在图谱中间的区域是 Java 和一些其它混合了这些特性的语言。它们提供对运行的硬件部分控制权，并且尝试尽量减少漏洞的出现。


Rust 有点不太一样，它并没有出现在这个图谱上。它同时提供给开发者安全性和控制权。


### Rust 的特性


Rust 是一种像 C/C++ 一样的系统编程语言，除此之外它还给予开发者对内存分配细粒度的控制。它不需要垃圾回收器。它的<ruby> 运行环境 <rp>  （ </rp> <rt>  runtime </rt> <rp>  ） </rp></ruby>很小，运行速度接近于在裸机上的运行。它为开发者提供了代码性能更大的保证。此外，任何了解 C/C++ 的人都能读懂以及编写 Rust 的代码。


Rust 的运行速度非常快，因为它是一种编译语言。它使用 LLVM 作为编译器的后端，并且还可以利用一大堆优化。在许多领域，它的性能都要高于 C/C++。它像 JavaScript、Ruby 和 Python 一样，与生俱来就是安全的，这意味着它们不会造成内存区块错误、<ruby> 野指针 <rp>  （ </rp> <rt>  dangling pointers </rt> <rp>  ） </rp></ruby>或者<ruby> 空指针 <rp>  （ </rp> <rt>  null pointers </rt> <rp>  ） </rp></ruby>。


另外一个很重要的特性就是消除数据竞争。如今，大多数计算机都具有多个核心，许多线程并发运行。然而，开发者很难编写好的并行代码，因此这个特性除去了他们的后顾之忧。Rust 使用两个关键概念来消除数据竞争：


* <ruby> 所有权 <rp>  （ </rp> <rt>  Ownership </rt> <rp>  ） </rp></ruby>。每一个变量都被移动到一个新的位置，并防止通过先前的位置来引用它。每一个数据块只有一个所有者。
* <ruby> 借用 <rp>  （ </rp> <rt>  Borrowing </rt> <rp>  ） </rp></ruby>。被拥有的值可以借用，以允许在一段时间内使用。


### 在 Fedora 24 和 25 上使用 Rust


若要开始使用，只需安装软件包：



```
sudo dnf install rust

```

示例代码 `helloworld.rs`：



```
fn main() {
    println!("Hello, Rust is running on Fedora 25 Alpha!");
}

```

 编译并执行：



```
rustc helloworld.rs
./helloworld

```

在 Fedora 上可以执行以下命令来安装最新的测试版本：



```
sudo dnf --enablerepo=updates-testing --refresh --best install rust

```



---


via: <https://fedoramagazine.org/rust-meets-fedora/>


作者：[Sumantro Mukherjee](https://fedoramagazine.org/author/sumantrom/) 译者：[OneNewLife](https://github.com/OneNewLife) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
## What is Rust?

Rust is a system programming language which runs blazingly fast, and prevents almost all crashes, [segfaults](https://wikipedia.org/wiki/Segmentation_fault), and data races. You might wonder exactly why yet another programming language is useful, since there are already so many of them. This article aims to explain why.

**Safety vs. control**

You may have seen a diagram of the above spectrum. On one side there’s C/C++, which has more control of the hardware it’s running on. Therefore it lets the developer optimize performance by executing finer control over the generated machine code. However, this isn’t very safe; it’s easier to cause a segfault, or security bugs like [Heartbleed](https://fedoramagazine.org/update-on-cve-2014-0160-aka-heartbleed/).

On the other hand, there are languages like Python, Ruby, and JavaScript where the developer has little control but creates safer code. The code can’t generate a segfault, although it can generate exceptions which are fairly safe and contained.

Somewhere in the middle, there’s Java and a few other languages which are a mixture of these characteristics. They offer some control of the hardware they run on but try to minimize vulnerabilities.

Rust is a bit different, and doesn’t fall in this spectrum. Instead it gives the developer both safety and control.

## Specialties of Rust

Rust is a system programming language like C/C++, except that it gives the developer fine grained control over memory allocations. A garbage collector is not required. It has a minimal runtime, and runs very close to the bare metal. The developer has greater guarantees about the performance of the code. Furthermore, anyone who knows C/C++ can understand and write code for this language.

Rust runs blazingly fast, since it’s a compiled language. It uses LLVM as the compiler backend and can tap into a large suite of optimizations. In many areas it can perform better than C/C++. Like JavaScript, Ruby, and Python, it’s safe by default, meaning it doesn’t cause segfaults, dangling pointers, or null pointers.

Another important feature is the elimination of data races. Nowadays, most computers have multiple cores and many threads running in parallel. However it’s tough for developers to write good parallel code, so this feature removes that necessity. There are two key concepts Rust uses to eliminate data races:

*Ownership.*Each variable is*moved*to a new location, and prevents the previous location from using it. There is only*one*owner of each piece of data.*Borrowing.*Owned values can be*borrowed*to allow usage for a certain period of time.

## Rust in Fedora 24 and 25

To get started, just install the package:

sudo dnf install rust

Here’s a demo program you can create. Edit a file with this content called *helloworld.rs* on your system:

fn main() { println!("Hello, Rust is running on Fedora 25 Alpha!"); }

Then use *rustc* to compile the program and run the resulting executable:

rustc helloworld.rs ./helloworld

## Contributing to Rust testing

Run the following command to install the latest testing version on Fedora:

sudo dnf --enablerepo=updates-testing --refresh --best install rust

Drop us a mail at test@lists.fedoraproject.org or #fedora-qa on IRC Freenode to get started!

Featured image based off this[ image from Unsplash](https://unsplash.com/photos/AimtjivBsmo)

## suhail ansari

It is good that Rust has good performance and making developers productive however Swift language is arguably better and more productive compared to Rust.

## suhail ansari

Rust has good performance and it is a good alternative to c/c++, however Swift language is arguably better and more productive compared to Rust.

## Paul

Your compilation command has the wrong extension on the hello world program.

## Paul W. Frields

@Paul: Thanks for catching this. Fixed now.

## Radesh Singh

Since I’m spending time on both ends of the spectrum (as you’ve presented it)…

C++ for certain tasks and Python for others… I’ve got to check out rust and see how it might unify my work. Thanks for sharing!

## Steve

It is an interesting language, and when used with cargo (which you need to install separately in F24), it seems quite usable. I’m checking out “The Book” right now. Thanks for letting us know.

## myomorpha

No such free lunch! What Rust excels at at the expense of readability. Languange trade-off always there.

## Steve

The same argument can be applied to any given programming language, trade off of human readability, safety of memory handling, crash potential, and control capability. I think Rust does have some place when considering greater control with the intent of stability, at the cost of readability maybe. Does that negate it’s place in the ecosystem though? I don’t think so. Perhaps we should return to the days when we did most cool things in machine code/mnemonics. I know that programming of computers, and other digital devices, tends to generate loyalty, usually proportional to time spent learning and becoming expert at it. For my part I apply much effort in trying to objectively look at languages as to their usability in any given area of application. Yet, I would lean towards the languages I am most comfortable and confident using, at least initially when considering a particular project for a customer.

## Nate

What’s the benefits of using Rust vs. Go?

## Mik

What’s the benefits of using Go vs. Rust ?

## Nate

That’s what I’d like to know!

## Frank Bergmann

Well, every few years some new guys appear at the horizon and claim that their new programming language is the holy grail.

And they mostly say that their language is fast. Perl did. Python did. Java did. Scala did. And now Rust.

It seems that this could be true for Rust because there is only a small “runtime overhead”. And it is compiled! Yeah!

But just take a look at the helloworld from above: A look at filesizes of comparable binaries created with C and ASM.

And then check the speed of this “program” which is actually a syscall:

[fff@book fff]# ls -l helloworld.*

-rw-r–r–. 1 fff fff 463 Sep 25 19:29 helloworld.asm

-rw-r–r–. 1 fff fff 79 Sep 25 19:14 helloworld.c

-rw-r–r–. 1 fff fff 68 Sep 25 19:14 helloworld.rs

[fff@book fff]# ls -l helloworld-*

-rwxr-xr-x. 1 fff fff 149 Sep 25 19:29 helloworld-asm

-rwxr-xr-x. 1 fff fff 6888 Sep 25 19:20 helloworld-dietlibc

-rwxr-xr-x. 1 fff fff 4115 Sep 25 19:22 helloworld-dietlibc-stripped

-rwxr-xr-x. 1 fff fff 7380 Sep 25 19:17 helloworld-glibc

-rwxr-xr-x. 1 fff fff 4116 Sep 25 19:22 helloworld-glibc-stripped

-rwxr-xr-x. 1 fff fff 182636 Sep 25 19:12 helloworld-rs

-rwxr-xr-x. 1 fff fff 127307 Sep 25 19:22 helloworld-rs-stripped

[fff@book fff]# for f in helloworld-rs helloworld-rs-stripped helloworld-glibc helloworld-glibc-stripped helloworld-dietlibc helloworld-dietlibc-stripped helloworld-asm;do echo $f:;./$f;done

helloworld-rs:

Hello, Rust is running on Fedora 25 Alpha!

helloworld-rs-stripped:

Hello, Rust is running on Fedora 25 Alpha!

helloworld-glibc:

Hello, C is running on Fedora 24!

helloworld-glibc-stripped:

Hello, C is running on Fedora 24!

helloworld-dietlibc:

Hello, C is running on Fedora 24!

helloworld-dietlibc-stripped:

Hello, C is running on Fedora 24!

helloworld-asm:

Hello, ASM is running on Fedora 24!

[fff@book fff]# for f in helloworld-rs helloworld-rs-stripped helloworld-glibc helloworld-glibc-stripped helloworld-dietlibc helloworld-dietlibc-stripped helloworld-asm;do echo $f:;time ./repeat_10000 $(pwd)/$f >/dev/null;done

helloworld-rs:

real 0m11.568s

user 0m1.865s

sys 0m4.233s

helloworld-rs-stripped:

real 0m11.599s

user 0m1.897s

sys 0m4.307s

helloworld-glibc:

real 0m6.627s

user 0m0.024s

sys 0m0.686s

helloworld-glibc-stripped:

real 0m6.595s

user 0m0.018s

sys 0m0.691s

helloworld-dietlibc:

real 0m2.103s

user 0m0.012s

sys 0m0.500s

helloworld-dietlibc-stripped:

real 0m1.878s

user 0m0.016s

sys 0m0.465s

helloworld-asm:

real 0m1.722s

user 0m0.012s

sys 0m0.458s

Well…

## Nate

Very interesting, thanks for sharing. Can I ask what are the differences between the ‘stripped’ and ‘non-stripped’ versions of the programs are?

## Frank Bergmann

Well, what do you think? 😉

strip - Discard symbols from object files.

GNU strip discards all symbols from object files objfile. The list of object files may include archives. At least one object file must be given.

strip modifies the files named in its argument, rather than writing modified copies under different names.

And “file” says about the “C-Lib files”:

helloworld-dietlibc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, BuildID[sha1]=6e99f6746d206420683a31f4ff0bb61f56982194, not stripped

helloworld-dietlibc-stripped: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), statically linked, stripped

helloworld-glibc: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=d1420598474abca3a20268ddb11afe47ba1a8dc8, not stripped

helloworld-glibc-stripped: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux.so.2, stripped

You see that “stripped” is a common term.

## Ken

Typically, stripped binaries remove any extraneous bits from the compiled binary – typically debugging symbols and such.

non-stripped binaries include all debugging symbols plus a few extras.

## Jason Knight

The issue of rust binary sizes is thoroughly explored in this excellent post: https://lifthrasiir.github.io/rustlog/why-is-a-rust-executable-large.html TL;DR if you want to remove jemalloc and libunwind, then your binaries can/will be comparable to C/C++.

## Yusuf Karabulut

Neither Perl, nor Python have ever tried to be C/C++ in speed, although Java or ‘Scala’ have some impressive use cases, where it can beat C, so did they.

Comparing hello-world programs speed Vs. real world applications is like trying to convince a C programmer to use a superior C++ (like this: https://youtube.com/watch?v=D7Sd8A6_fYU)

How about something actually more realistic like this: http://blog.burntsushi.net/ripgrep/

Some people won’t eat anything they’ve never seen before.

## test

rust seems to be faster

https://benchmarksgame.alioth.debian.org/u64q/compare.php?lang=rust&lang2=go

some comparisions

http://dave.cheney.net/2015/07/02/why-go-and-rust-are-not-competitors

https://www.quora.com/How-do-Rust-and-Go-compare-1