vbrandl.net/content/post/a-rusty-weekend.md

60 lines
4.5 KiB
Markdown
Raw Normal View History

2017-04-07 13:35:48 +02:00
+++
2017-04-07 14:07:15 +02:00
date = "2017-04-07T14:05:00+02:00"
publishdate = "2017-04-07T14:05:00+02:00"
2017-04-07 13:35:48 +02:00
title = "A Rusty Weekend"
categories = ["programming", "rust"]
2017-04-07 14:07:50 +02:00
draft = false
2017-04-07 13:35:48 +02:00
description = "My first steps with the Rust programming language"
tags = ["rust","skeleton"]
+++
## The Rust Language
2017-04-07 14:07:15 +02:00
Since reading about Mozilla's new programming language [Rust][0] I was eager to give it a try. Rust is a really new language and the first stable version was released in 2015.
2017-04-07 13:35:48 +02:00
Almost C like performance without memory corruption vulnerabilities, no race conditions and so forth thanks to a number of very interesting design concepts
and no garbage collector or any other kind of runtime overhead made that language sound really awesome.
2017-04-07 14:07:15 +02:00
In Rust all safety checks are done in compile time. The compiler translates into the [LLVM][1] meta language which then takes care
of the optimization and compilation into machine code for the target architecture. Rust libraries provide a [FFI][2]
2017-04-07 13:35:48 +02:00
so they can be used from almost every other language. Due to it's safety features and the lack of performance overhead, Rust also aims to be a system programming language and there are
2017-04-07 14:07:15 +02:00
some really awesome projects, e.g. the [Redox operating system][3] which implements a microkernel architecture and has some really great design ideas (have a look at it!).
Also Mozilla started implementing a new, parallel browser engine called [Servo][4] which is more than 3 times faster than Gecko, the current engine used in Firefox[^servo].
The MP4 parser in Firefox is already implemented in Rust and there is more to come. [Here][5] is a more complete list of Rust code that runs on production systems.
2017-04-07 13:35:48 +02:00
2017-04-07 14:07:15 +02:00
Rust also has its own package manager/build management tool called [cargo][6] that makes working with libraries really easy.
Libraries in the Rust ecosystem are hosted on [crates.io][7] and you just need to reference them in the cargo configuration and they will be downloaded and included in the build.
2017-04-07 13:35:48 +02:00
## New Projects
2017-04-07 14:07:15 +02:00
Last weekend I started my first project to get in contact with Rust, called [skeleton][8], which aims to be a language independent project management tool. When starting a new project, e.g. using
2017-04-07 13:35:48 +02:00
Gradle for build management, one initializes the structure using `gradle init` but in most cases that's not the only thing to do. You might want to initialize a git repository for the project, maybe copy a license file
and download a gitignore file. Skeleton aims to automate these steps using some simple configuration files which allow the user to execute commands, create folders, touch files, include other skeleton configurations
2017-04-07 14:07:15 +02:00
or download a gitignore file from [gitignore.io][9] by a given list of languages, IDEs, ... to ignore. So `skeleton --lang=java init` might initialize a Gradle project, copy your license file, touch your
README.md, initialize a git repository and download a gitignore file for [Java, Gradle and IntelliJ][10].
2017-04-07 13:35:48 +02:00
## Conclusion
The design of Rust makes you think different about your code, e.g. by default every variable in Rust is immutable. If you want a mutable variable you have to explicitly mark it as such. When passing a variable as a parameter to
2017-04-07 14:07:15 +02:00
a function you can't simply reuse it after the function returns, as long as you don't explicitly borrow the variable for the time of the function call. All these features can also make you think different when writing C code
and probably help producing better code. I really think Rust is worth looking at especially when implementing performance critical parts of a project. Also when looking for a new language to learn it is a good idea.
2017-04-07 13:35:48 +02:00
Even for people coming from scripting languages who aren't familiar with the edit-compile-run cycle of compiled languages it is really easy thanks to cargo.
2017-04-07 14:07:15 +02:00
Write your code, maybe add some dependencies and simply run `cargo test` for your tests or `cargo run` to execute the program. To get started I can recommend the official documentation [The Book][11] or have a look at some
examples on [Rust by Example][12].
2017-04-07 13:35:48 +02:00
[^servo]: http://events.linuxfoundation.org/sites/events/files/slides/LinuxConEU2014.pdf
2017-04-07 14:07:15 +02:00
[0]: https://www.rust-lang.org/
[1]: https://en.wikipedia.org/wiki/LLVM
[2]: https://en.wikipedia.org/wiki/Foreign_function_interface
[3]: https://www.redox-os.org/
[4]: https://servo.org/
[5]: https://www.rust-lang.org/en-US/friends.html
[6]: https://github.com/rust-lang/cargo
[7]: https://crates.io/
[8]: https://github.com/ntzwrk/skeleton/
[9]: https://gitignore.io
[10]: https://www.gitignore.io/api/java%2Cgradle%2Cintellij
[11]: https://doc.rust-lang.org/book/
[12]: http://rustbyexample.com/