Learn more about Israeli war crimes in Gaza, funded by the USA, Germany, the UK and others.

Hello world in Rust

I’m tinkering with Rust. Here’s how I started.

Rust has a classic “pipe curl to shell” installer: curl https://sh.rustup.rs -sSf | sh. The installer works out what platform I’m on. It downloads some stuff and puts it in /Users/jim/.cargo. “Cargo” is Rust’s package manager. It added export PATH="$HOME/.cargo/bin:$PATH" to my ~/.bash_profile, so now I have access to some binaries there, including cargo (the package manager), rustup (a Rust version manager, so you don’t have to run that curl-to-shell installer every time) and rustc, the Rust compiler.

To test rustc, I created hello.rs:

fn main() {
    println!("Hello World!");
}
$ rustc hello.rs
$ ./hello
Hello World!

We can compare this file to the equivalent in C:

#include <stdio.h>
int main() {
  printf("Hello world!\n");
  return 0;
}

The Rust binary has an overhead of around 420K. I don’t know what’s in there!

Both binaries depend on /usr/lib/libSystem.B.dylib, the macOS system library. This provides, amongst other things, the C standard library. The Rust binary has one other dependency, /usr/lib/libresolv.9.dylib. This provides a DNS client, includable in C with resolv.h, linkable in C with -lresolv, and documented in man 3 resolver. It’s not clear why the Rust binary depends on this.

Tagged .

Similar posts

More by Jim

Want to build a fantastic product using LLMs? I work at Granola where we're building the future IDE for knowledge work. Come and work with us! Read more or get in touch!

This page copyright James Fisher 2017. Content is not associated with my employer. Found an error? Edit this page.