@kazhik's answer will work for Raspberry Pi 2s and 3s (which are ARMv7/8 based), but not for Raspberry Pi 1s or Zeros (which are ARMv6 based).
The problem is that Debian/Ubuntu's armhf
port (and thus their gcc-arm-linux-gnueabihf
package/compiler/toolchain) targets >= ARMv7.
Fortunately, rustup's gcc-arm-linux-gnueabihf
targets >= ARMv6 (with hardware floating-point, which all Raspberry Pis support), so all that's needed is the correct linker. The Raspberry Pi foundation provides one of those in their tools repository.
Putting it together, the following steps can be used to cross compile a Rust binary that works on all Raspberry Pis:
$ rustup target add arm-unknown-linux-gnueabihf
$ git clone --depth=1 https://github.com/raspberrypi/tools raspberrypi-tools
$ echo "[target.arm-unknown-linux-gnueabihf]" >> ~/.cargo/config
$ echo "linker = \"$(pwd)/raspberrypi-tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-gcc\"" >> ~/.cargo/config
To test the cross-compiler (assuming a Pi is running and reachable with the default raspberrypi
hostname):
cpick@devhost: $ cargo new --bin rpi-test
cpick@devhost: $ cd rpi-test
cpick@devhost: $ cargo build --target=arm-unknown-linux-gnueabihf
cpick@devhost: $ scp target/arm-unknown-linux-gnueabihf/debug/rpi-test pi@raspberrypi:
cpick@devhost: $ ssh pi@raspberrypi
pi@raspberrypi: $ ./rpi-test
Hello, world!