The Rust compiler is not (yet?) distributed as a cross-compiler for the Raspberry Pi. However, it can be compiled as a cross-compiler from source code with the Raspberry Pi development tools.
That sounds like hard work, but don’t worry — the Rust build process is completely automated. It does take a long time to run, but won’t require any effort on your part.
You’ll need Git to download the Raspberry Pi tools and the source code of the Rust compiler. Git is available in the package managers for most Linux distributions. For example, on Ubuntu or Debian you can install it with the command:
% sudo apt-get install git
If Git is not available in your package manager, you can download it from http://git-scm.com/download/.
When Git is installed, you can download the Raspberry Pi development tools. The following command downloads the tools into a directory named pi-tools in your home directory:
% git clone https://github.com/raspberrypi/tools.git ~/pi-tools
Now, download the Rust compiler source code and jump into cloned directory:
% git clone https://github.com/rust-lang/rust.git rust && cd $_
Add the Raspberry Pi tools to your shell’s command search path:
% export PATH=~/pi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian/bin:$PATH
Then build Rust as a cross-compiler. The following command will build the cross compiler and install it into a directory called pi-rust in your home directory.
% ./configure --target=arm-unknown-linux-gnueabihf --prefix=$HOME/pi-rust && make && make install
The build will take a long time to complete. When it’s finished, you can test it by compiling a simple Rust program and running it on the Pi. But until then, you’ve got more than enough time to go have a cup of tea. Or two.
To test the cross-compiler and deployment of compiled programs onto the Raspberry Pi, we’ll compile a traditional Hello World program. Here’s Hello World in Rust:
link:../src/hello.rs[role=include]
Save this code into a file called hello.rs.
To compile it for the Pi, make sure that the Raspberry Pi tools are on your path (as shown above), then run the command:
% ~/pi-rust/bin/rustc --target=arm-unknown-linux-gnueabihf -C linker=arm-linux-gnueabihf-g++ hello.rs
This will produce an executable program called hello
. You won’t be
able to run it on your PC. You have to deploy it onto the Pi and run
it there.
We’ll look at how to do that in the next chapter.