What is Rust?
Rust is a system programming language which runs blazingly fast, and prevents almost all crashes, segfaults, 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.
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
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