
Learning Rust - rogual
http://foon.uk/rust.txt
======
Animats
Rust is a potentially important language with weak, and badly organized,
documentation. The "30 minute introduction" starts by introducing the _package
manager_ , manifests, crates, and lock files. But it's easy. It says so,
twice! "You should feel right at home if you've used tools like Bundler, npm,
or pip."

Rust is the best hope we have of fixing the legacy mess at the bottom left
from C/C++. I really hope the Mozilla crowd doesn't fuck up.

~~~
dbaupp
_> The "30 minute introduction" starts by introducing the package manager,
manifests, crates, and lock files. But it's easy. It says so, twice! "You
should feel right at home if you've used tools like Bundler, npm, or pip."_

Presumably you're implying something is not easy... But I don't understand
why? That is, why does introducing the package manager (I.e. allowing people
to jump straight in with nicer tooling) make the language not easy?

~~~
tormeh
Because beginners generally don't care about tooling, external libraries etc.
They want a compiler (and preferrably a REPL) and some language pointers and
then write a program that searches for primes or whatever.

~~~
shadowmint
There are lots of tutorials; if you want to dive straight into the code look
at 'rust by example'. You dont even have to install rust to use it.

Also, REPL is a cute toy, but absolutely not important and hard to do for
compiled languages. If you want one, you can run your own copy of play.rust-
lang.org; its actually pretty good.

~~~
ketralnis
I get that it's hard for compiled languages and I've learned to live without
one for many of them. But "a cute toy, but absolutely not important" is way,
way off the mark. When available, a REPL is a godsend and has saved me
personally _enormous_ amounts of time.

Besides, there do exist compiled languages with REPLs (Haskell, Scheme, Common
Lisp). Even Python has to compile the textual code into bytecode and execute
that.

~~~
shadowmint
I'm happy to accept 'relatively unimportant compared to other issues with
rust'.

REPL is a useful learning tool, but its also a not relevant to the majority of
tasks for rust. Rust is for large complex tasks. If you want a scripting
language, use python.

~~~
ufo
REPLs aren't just for learning- they are great for exploring and interactively
designing programs. You are also conflating REPLs with scripting languages -
the parent post already has a bunch of examples of languages suited for
complex tasks that make very good use of their REPLs.

~~~
shadowmint
They're really not _in this case_.

play.rust-lang.org is a bite sized code compile \ execute environment that
lets you play, explore, delect different compilers and share failed build to
get help. Its fantastically useful ... but its _not_ a REPL, and being one
would in no way make it more useful.

REPLs are useful where a few lines can make a meaningful program.

That is not the case in rust.

~~~
tormeh
I use the REPL to try out syntax I don't quite remember and to ascertain
myself of how the libraries work, something that I have to do often. It's not
an absolute necessity, but it makes coding a lot faster.

------
rogual
I thought it might be useful to document my experiences starting out with
Rust, in the same vein as Artyom's "Learning Racket" series
([http://artyom.me/learning-racket-1](http://artyom.me/learning-racket-1)),
which I got a lot out of.

I really liked the style of that series when investigating Racket myself. That
is, little to no preparation, and writing down everything you try, even if it
doesn't work.

Hope someone finds it useful.

~~~
th0br0
I'd strongly recommend using Rust's nightlies. [http://www.rust-
lang.org/install.html](http://www.rust-lang.org/install.html) \- they've even
got pkg installers for Mac.

~~~
sanderjd
You can also get the rust and cargo nightlies using homebrew-cask[0], which I
find a bit easier (though I'm not exactly sure why).

[0]: [http://caskroom.io/](http://caskroom.io/)

~~~
bjz_
I've been using my own `rustup` script, but this is much nicer!

~~~
sanderjd
I actually use it with a `rustup` alias, because homebrew-cask isn't great at
upgrading nightlies:

    
    
        alias rustup='brew cask fetch --force rust cargo && brew cask install --force rust cargo && git -C ~/.vim/bundle/rust.vim pull'

------
hahainternet
Rust is one of those languages I'm excited to learn, in about 2 years time.

It's a shame there are so many comparisons between Go and Rust because it does
both a disservice. Rust is still very much unfinished and still being
developed, but despite this it is achieving its goals of complete safety and
compile time checking.

It's an admirable project, but it's got a lot of syntax fiddling and
redesigning to go before I think it's 'prime time' ready.

~~~
pcwalton
> It's an admirable project, but it's got a lot of syntax fiddling and
> redesigning to go before I think it's 'prime time' ready.

We aren't doing much syntax fiddling and redesigning anymore. 1.0, which is
very soon, will have a stability promise and will be backwards compatible.

~~~
bobcostas55
That's disheartening, it looks like a nonsensical mix of about a dozen
different languages right now. Are naming guidelines and predictable, coherent
choices just not hip enough in 2014?

~~~
pcwalton
Rust has a comprehensive set of naming guidelines: [https://github.com/rust-
lang/rust-guidelines/tree/master/sty...](https://github.com/rust-lang/rust-
guidelines/tree/master/style/naming)

You can browse around that repository for many other guidelines designed to
enforce predictable, coherent choices.

------
amelius
I'm interested in Rust also. But I'm wondering, does it have these features,
and to what extent?

    
    
      * coroutines
      * pure functional programming (enforcing it in an isolated subset of the code)
      * laziness
      * (clean) meta-programming
      * introspection
      * garbage collection
    

Especially for stream-based programming (essential for internet applications),
you'd need something like coroutines, to keep things manageable. I'm wondering
if there is example code for a well-performing Rust-based web-server out there
already.

Also, how does Rust compare to other modern languages, like Scala?

~~~
mattdw
I assume coroutines could be trivially implemented with sync_channel(0), which
blocks the sender until the receiver takes the message. (And the receiver
blocks until there is a message available.)

The 'generator' use of coroutines could be implemented with Receiver::iter,
which is an iterator over a channel.

(As I understand it, coroutines are just a special case of channels.)

[Edit: see <[http://doc.rust-lang.org/std/comm/>](http://doc.rust-
lang.org/std/comm/>) for channel docs]

~~~
amelius
Wouldn't that require the use of threads, and hence be very expensive?

------
msie
I love it! I'm sure everyone has had the idea of logging all their troubles
but this guy has the fortitude to do it. It is troubling to see that Rust
still has a way to go in terms of learning it. I was trying to learn Rust 1.5
years ago and ran into enough problems that I gave up. I will try again this
week...

~~~
bjz_
> It is troubling to see that Rust still has a way to go in terms of learning
> it.

Have no fear! Rust 1.0 is not far off and things will become a lot better
then. Just be aware that google is pretty useless with now. You are much
better off with the official docs:

\- [http://doc.rust-lang.org/guide.html](http://doc.rust-lang.org/guide.html)

\- [http://doc.rust-lang.org/std/index.html](http://doc.rust-
lang.org/std/index.html)

Also, be sure to use `irc.mozilla.org #rust`. We are very friendly and would
be glad to help you out if you are stuck.

------
nkcpiytdhauwuhc
Please stop using 0.11. It's a really old dialect or rust. Just work out of
rust nightly, and use the docs on the [http://rust-lang.org/](http://rust-
lang.org/) to learn.

------
kaoD
I understand the frustration while learning Rust. As a Rust beginner myself, I
suggest not to follow guides (except the ones at [http://www.rust-
lang.org](http://www.rust-lang.org)) since most are outdated.

My way to learn Rust: I spend 90% of the time in Rust's std docs, 9% of the
time watching the type/borrow checker spit at me and 1% of the time in IRC
when I'm out of ideas. Google was useless most of the time.

I found revisiting [http://doc.rust-lang.org/index.html](http://doc.rust-
lang.org/index.html) (specially the spec and the first four guides) as I was
getting insights from experimenting with the language allowed to me to
understand what was going on.

For me the most important parts to understand to get up to speed were:

\- The module system (understanding `mod` vs. `use`).

\- The concept of ownership. References. Lifetimes. Borrows. Copies. Moves.
Moving out of. `ref` on tuples.

\- Getting used to rustdoc's format. Docs hard to navigate not knowing the
language idiosyncrasies. Trait methods are often not textually documented.

\- Understanding rustc error reporting. IMHO the worst part of rustc is the
cryptic lifetime error messages (though they're getting better).

Good luck in your journey! I'm loving Rust so far.

\---

Some concrete feedback:

> This probably isn't right (I'm sure empty maps must be supported somehow),
> but it will do for now.

Indeed. I see you joined the IRC (great idea, people are very helpful there)
and got the correct syntax, but to fix the ~str problem, the answer is:

    
    
       let mut map = HashMap::<Box<str>, uint>::new();
    

Box<A> is the resulting type of an expression "box a" where `a` is of type A.
I guess an example will make it more clear:

    
    
       let owned_three: Box<uint> = box 3;
    

But Box<str> is actually just String, so the final line should read:

    
    
       let mut map = HashMap::<String, uint>::new();
    

By the way... why do you need to box the str?

Local bindings (args, let bindings...) are allocated on the stack and must
have a fixed size. A string has variable size by definition, but a box has a
known size (the size of a reference to your heap-allocated str), so can can
wrap the str (on the heap) on a box (on the stack).

That's why you'll never see a sole `str` as a type: you either see the (boxed)
String or &str (reference to a string slice).

> Ah, OK. So I think this means Rust has no built-in way of determining if two
> int-arrays are equal.

Actually the problem is Rust currently doesn't implement traits for static-
sized arrays because the size is part of the type signature and each size
would need to be implemented separately.

[https://github.com/rust-lang/rust/issues/7622](https://github.com/rust-
lang/rust/issues/7622)

Though the Coord solution you implemented is in my opinion better for a typed
language.

~~~
lostdog
> As a Rust beginner myself, I suggest not to follow guides (except the ones
> at [http://www.rust-lang.org](http://www.rust-lang.org)) since most are
> outdated.

Nearly every (external) guide I've tried to use is out of date, but it's
impossible to tell from the guide itself. I wish everyone who wrote a guide or
tutorial would label it with the version of Rust it works with (or at least
the date).

~~~
bjz_
Yeah, we highly recommend blog post authors to put their `rustc -v` at the top
of the page, at least until 1.0 (which is not far off).

------
Fede_V
This was really helpful - it's nice to see that he takes the same steps that I
do when learning a new language.

Interestingly, this highlights just how incredibly important up-to-date and
authoritative tutorials/docs just are. Julia/scikit-learn are both absolutely
amazing in this regard.

~~~
bjz_
The official docs are much better - they are under tests and you can't make a
change to the compiler/libraries without updating them.

[http://doc.rust-lang.org/guide.html](http://doc.rust-lang.org/guide.html)

------
_random_
Syntax colouring would greatly improve readability.

I completely understand author's frustration. Modern language is done once
there is something like ReSharper for it. Rust is far from being done.

~~~
Argorak
As a multi-lingualist, I don't agree. Languages should stand for themselves
without tooling.

~~~
marvy
Some languages should, some should not. Probably a language like Rust should.
Asking that of Smalltalk may be too much. But every language can benefit from
tooling, even if it doesn't need it. For instance, do you know of any language
more 15 years old that still has no debugger, let alone syntax highlighting?

~~~
Argorak
ReSharper and a debugger a completely different pair of shoes. I agree that
you need a debugger (and Rust has one, gdb), but I don't agree that it needs
refactoring tools.

~~~
BrandonM
Good IDE support is the difference between constantly hunting through (or
thoroughly memorizing) documentation and having a program help you with it.
It's also great for merging the edit and compile steps so that you can correct
errors in realtime. Refactoring tools are nice but are not the strongest
selling point for IDEs.

I recall reading old Rust design considerations where they decided to stick
with the `foo.bar` syntax because they like the way IDEs can autocomplete with
documentation after typing the dot.

------
alkonaut
This is exactly why a language needs to come with good IDE support. Compiler,
libraries and documentation are only 3 of the 4 bits you need.

~~~
bjz_
Racer is looking pretty great - would be good to see it working with some
IDEs: [https://github.com/phildawes/racer](https://github.com/phildawes/racer)

