[mini] Hello, Rust! #1.5 – Random links

Previous post.

I’ve collected a few links and resources about the language in those last days. So this is just a small “flush” from my personal links and notes into an organized post.

  • http://rosettacode.org/wiki/Rust: Rosetta is a nice place to compare several programming languages, and to discover new ones too. However, I’ve found out that it is a poor place to learn about Rust in general, because most of the code in there is written in versions of Rust pre-1.0, which means that many of them don’t (won’t) compile with the current version family of Rust (1.x).
  • https://news.ycombinator.com/item?id=9711367: a pretty typical Hacker News thread. Nice comments about Rust (despite of the topic being about Golang).
  • http://arewewebyet.com/: well, Rust intends to be programmable for the Web a day. After all, Mozilla sponsors it…[1]. I don’t expect Rust to be like Rails or Python in the future in the field of web programming; instead, it will become probably something along the lines of Go.
  • http://chrismorgan.info/blog/rust-fizzbuzz.html: this article introduces a few features about the Rust compiler. It is a good read for a typical C++ programmer. Some things in the language won’t work as expected.
  • https://graydon2.dreamwidth.org/214016.html: a glance about Rust evolution over the years.
  • http://siciarz.net/24-days-rust-cargo-and-cratesio/: this guy did a series of posts about Rust, something like what I’m doing right now. However, this is from pre-1.0 — older versions of Rust [2].
  • https://github.com/phildawes/racer: autocompletion for your favorite text editor. It is in beta stage and does not work with a simple ‘git clone’, so I’m skipping it for now. You can test ‘cargo’ (see the previous post) with this crate, for example, to search for it or to build a release from it.
  • https://github.com/brson/multirust: a tool to manage  multiple versions of rust. Ruby users might find this familiar (rbenv, rvm).
  • https://github.com/kud1ing/awesome-rust: an [pretty tipical] awesome list of resources about Rust.

Thanks Francisco and Vinicus for a few of those links.

Next post.

Footnotes

  • [1]: technically, it is not because of this fact; however, it contributes too.
  • [2]: also, I probably won’t write too many posts as him, as the objective is just to get an overview of the language.
[mini] Hello, Rust! #1.5 – Random links

Hello, Rust! #1 – Cargo: the package manager

Continuation of this post.

Post #0 was verbose, but from now on I’ll try to go straight to the point. Comments either too verbose or out of scope will usually be placed on footnotes, like this one [1], so it’s up to you to choose if you will read them or not.

Cargo is the de-facto — and standard — rust package manager. it is not only a package manager, but a “build manager” too. So let’s analyze what this means:

(i): build manager

Think of it as C/C++ cmake, or make, or Scala’s SBT (Simple Build Tool). You can put your project metadata and its build dependencies on a file which cargo can read and do cool stuff about it. This file is Cargo.toml.

I remember seeing this TOML thing before: https://github.com/toml-lang/toml

It is like a JSON or a YAML file; or, even better for our analogy purposes, like a ‘.ini’ file. The contents of this file are just key/pair values, divided in sections (or subsections, if you want to call them this way).

The default Cargo.toml file that is created for me after I create a new Rust project with

cargo new --vcs git --bin sudoku

is simply

[package]
name = "sudoku"
version = "0.1.0"
authors = ["Thiago Perrotta "]

Note that the ‘authors’ value was extracted from my ~/.gitconfig file. So,

cargo build

will do pretty much what you expect. By default, it builds an executable in DEBUG mode. If you want a production version of your program, just add ‘–release’ to the previous command. Let’s look at our current directory structure now:

$ tree
.
├── Cargo.lock
├── Cargo.toml
├── src
│   └── main.rs
└── target
 └── debug
 ├── build
 ├── deps
 ├── examples
 ├── native
 └── sudoku

The build created the ‘target/debug’ directory. If you opted for the production version, a ‘target/release’ one would be created instead. Both can coexist at the same time. Now, if you run ‘cargo build’ again, it will finish faster than before, because there is nothing new to compile. I suppose cargo uses a mechanism similar to GNU Make, where compilations are done only if the source code is newer (in time) than its dependencies.

Okay, cargo looks like a good friend. We can do cargo {new, build, clean} and it looks enough. A tab complete on zsh or bash, however, will reveal several more subcommands that might be useful later on.  A few examples include:

  • cargo run ==> if you’re in a hurry (think of this as ‘scala main.scala’, instead of ‘scalac main.scala’)
  • cargo doc ==> we don’t have documentation yet, just a hello world
  • cargo test ==> yaaay, (unit?) test
  • cargo bench ==> this is like Golang…benchmarks
  • cargo publish ==> hmmmm…it looks like a way for us to create our own packages and maybe, later on, put them on a public repository (such as crates.io <== analogous to npm)

OK, let’s stop for now. Oh, just one more: ‘cargo search’ (!). A quick example…’cargo search regex’. It returns a few results. If you want to explore more, run ‘cargo –list’ to see all available commands.

(ii) package manager

I already said a few bits about it in the previous section: by being able to interact with the crates.io server, cargo also acts as Rust package manager. You can search for packages, download them, add them as build dependencies of your project, possibly specifying the version you want, and so on.

You can also use the same cargo command-line tool to publish packages to the official repository. It is really simple, just a matter of ‘cargo login’ first, then figuring out how to update your package 😉

For me, it looks better than python’s pip and comparable to ruby gems and node’s npm, but it is still too early to make any judgements.

Wrapping up

Okay, so cargo looks really simple yet efficient. It reminds me of ‘lein’ for clojure and ‘sbt’ for scala: it seems to have sane defaults so the rust programmer probably won’t struggle configuring the build system — now, this doesn’t mean that he won’t struggle with the compiler…well, I guess we are set up now and can live by ourselves without an IDE, at least for managing our future project.

Next post.

Footnotes

  • [1]: see? Also, I’m a learner, so I’ll describe the things the way I expect them to be. However, my description won’t always be 100% correct. So pay attention if you intend to use any of this information in production.
Hello, Rust! #1 – Cargo: the package manager

[Teaser] Hello, Rust! #0 — why??

During the last year I learned several  programming languages; most of them just for the sake of learning, with no specific purposes at all. I like to learn how programming languages work behind the scenes and what different (programming) paradigms are available today.

I should say that some of these languages are, today, completely buried into some distant place in my brain; I barely remember how to declare a simple vector with them — I mean, remembering out-of-the-box, without consulting external sources and references. This is usually a bad aspect regarding the human learning process and it tends to discourage further (future) learning and inspection. However, if I needed to work in any of these languages today, a few days would be probably more than enough to make me remember the fundamental aspects of those languages so I could start a new project with them.

So what? Here is the thing: I learned those languages with several (different) methods of learning: books, videos, reading other people code, etc. I’m still trying to optimize the way of learning those things [1], but anyway I will try it yet again, this time with Rust.

Really?

I don’t know. I tend to start those things but they can disappear without any trace. Other times they really succeed and become a stable series of posts. The last one was about FreeBSD.

Anyway, right now I’m kinda busy with college assignments and tests, so I’m not gonna post about Rust on a regular basis. This post is really just a teaser. However, if everything work according to my plans, then I will begin the third series (“challenge”) of this blog probably on the next month, and it will be about Rust. Period.

I define a “series” to be a success if I write at least 5 posts about it. The purpose is not to become an expert in the subject, but just to make it familiar and acquire a general know-how about it.

So, that said (consider those paragraphs a small disclaimer) — comments or private messages motivating me to begin this are very welcome.

C’mon, rust

OK, I don’t like a post to be completely useless, so I will install and learn how to compile Rust, plus write a hello world on it. Sounds fair?

I use Arch Linux, so I will use falconindy’s asp [2] to pull the Rust PKGBUILD so I can build/compile it manually.

$ ASPROOT="$HOME/.asp" asp export rust
$ cd "$HOME/.asp/rust" && makepkg -si

You should really read the PKGBUILD instead of simply makepkg’ing it.

Oh, the Hello World now, right?

fn main() {
    println!("Hello World")
}

Save this to a hello.rust file. Then, it feels like java/scala (well, it remembers me of javac and scalac…):

rustc hello.rust && ./hello

Curiously enough,

file hello.rust

yields

hello.rust: C source, ASCII text

All right. See you later on (I hope). But it won’t be soon.

Update: next post.

Footnotes

[Teaser] Hello, Rust! #0 — why??