2021-07-31

First Contact: Rust

#software

Context

So I came across an article on linux weekly news

Rust for Linux redux

and later

A GPIO driver in Rust

It is totally beyond me, why anyone would like to add yet another technology to such a gigantic project as the Linux kernel. It is also beyond me, why the possibility of a totally new class of problems arising from "domain crossing" between C and Rust is not considered more. I talked to a Rust fanboy the other evening, and he did not see anything risky. Oh, well.

I'm definitely not saying, Rust is a bad language. I have read the very interesting post by Graydon Hoare, which goes beyond transactional memory. Very inspirational!

What's next?

I would be all for writing a brand new kernel with new ideas and ideally getting rid of some of the old baggage along the way. There are several such experiments, Genode/SculptOS comes to mind, and Redox, of course.

genode.org

www.redox-os.org

So there is a slight but non-zero chance that I have to deal with Rust code, whether I like it or not. The only way to argue with the fanboys, is to be informed. So I set out to learn this new to me language.

Contact

So I installed the rust compiler on my noteboot running Debian 11, obtained a recent copy of the book

The Rust Programming Language

and started.

So a "Hello, world!" program is the agreed way to start. And I take the liberty to directly compare to C, because I'm halfway fluent in that language.

Rust hello_world

fn main() {
    println!("Howdy, mate!");
}

Compile command

rustc main.rs

C hello_world

#include <stdio.h>

int main(void) {

  printf("Howdy, mate!\n");
  return 0;
}

Compile command

gcc -o howdy hello_world.c

Some Gazing in Astonishment

Both pieces of code are very similar. I called both compilers without any flags. Trying to look innocent here. Both executables run and produce the expected output. However:

ew@dione:~/dione/Rust/hello_world$ file main
main: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=cb5c8c0e32b7fee9f0aaef0edf8690925a72330d, for GNU/Linux 3.2.0, with debug_info, not stripped

ew@dione:~/dione/C/hello_world$ file main
main: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=c32464ce423fc26da34bc4f2de40ee44678ccb1a, for GNU/Linux 3.2.0, not stripped

They are technically both "dynamically linked", and not stripped. Ok, the C executable lacks debug info. So let's fix this:

ew@dione:~/dione/C/hello_world$ gcc -g -o main2 main.c
ew@dione:~/dione/C/hello_world$ ls -l main2
-rwxr-xr-x 1 ew ew 17632 Jul 31 22:14 main2
ew@dione:~/dione/C/hello_world$ file main2
main2: ELF 64-bit LSB pie executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=71f34ec927797ec0f0e548cf11ceacbaeb4fa413, for GNU/Linux 3.2.0, with debug_info, not stripped

The size increases by 1024 Byte, 17kiB are still nowhere near 10MiB.

ew@dione:~/dione/Rust/hello_world$ ldd main
        linux-vdso.so.1 (0x00007ffd545a3000)
        libgcc_s.so.1 => /lib/x86_64-linux-gnu/libgcc_s.so.1 (0x00007f9a1df3c000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007f9a1dd77000)
        /lib64/ld-linux-x86-64.so.2 (0x00007f9a1dfac000)
        libpthread.so.0 => /lib/x86_64-linux-gnu/libpthread.so.0 (0x00007f9a1dd55000)
        libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007f9a1dd4f000)

ew@dione:~/dione/C/hello_world$ ldd main
        linux-vdso.so.1 (0x00007ffff116a000)
        libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007faff81a9000)
        /lib64/ld-linux-x86-64.so.2 (0x00007faff837f000)

The Rust executable links to libpthread and libdl by default. Aha. Interesting.

So while I'm technically able to edit, compile, and run Rust code on this machine, I'm nowhere near understanding, what's actually going on.

I'm sure there are reasons for all of this and more. So on with the book.

Home