

Rust for C++ programmers – part 1: Hello world - adamnemecek
http://featherweightmusings.blogspot.co.nz/2014/04/rust-for-c-programmers-part-1-hello.html

======
coldtea
God, I hate the single quote character.

Perhaps I've been conditioned by decades of languages with balanced quotes
(for strings etc), plus some OCD, I can't stand an open '.

~~~
mattgreenrocks
> &'static str

I'm hoping there's a more...friendly way to write that (I'm assuming that's
for literals).

Edit: ~str, see: [https://github.com/mozilla/rust/wiki/Rust-for-CXX-
programmer...](https://github.com/mozilla/rust/wiki/Rust-for-CXX-programmers)

~~~
steveklabnik
The friendliest way to write it is to simply not and let the compiler infer
it.

~~~
AnimalMuppet
That is in fact the best way... until it's not. If I understand correctly (and
I may not), there are times when you _have_ to write it.

~~~
dbaupp
You're correct; at the moment function signatures have to be written in full.

(This may be relaxed slightly in future, that said, it's very _very_ unlikely
to be loosened to full Haskell-style whole-program inference.)

------
dcsommer
I like this article. It's short and easy for the target audience and has
enough content to let C++ developers start to understand Rust. It also hints
at and leaves unexplained other features in the language, piqueing the
curiosity of the reader.

------
leoc
> You don't need the `return` keyword in Rust, if the last expression in a
> function body (or any other body, we'll see more of this later) is not
> finished with a semicolon, then it is the return value. So `foo` will always
> return "world". The `return` keyword still exists so we can do early
> returns. You can replace `"world"` with `return "world";` and it will have
> the same effect.

Is this a bit brittle for a language which is expected to have applications in
safety/security-critical coding? It's not hard to imagine side-effecting last
expressions with discarded return values being turned into (effective) return
statements by an omitted semicolon. Maybe the type system or other bits of the
language would prevent this?

~~~
pcwalton
The type system generally prevents this, because you get a type error if the
function return type is not as it's declared. More generally, it's not good
Rust style to ignore return values, and dropping a type that should not be
ignored (e.g. an error) causes the compiler to emit a warning.

~~~
sdegutis
> _it 's not good Rust style to ignore return values, and dropping a type that
> should not be ignored_

If I remember correctly, much of OpenSSL is not written in "good C style"
either. Considering that we're talking about "a language which is expected to
have applications in safety/security-critical coding", it seems like a
legitimate concern.

~~~
pcwalton
I knew I would get someone calling me out on "it's not good Rust style". This
style is _enforced_ by warnings for important types, such as errors.

The semicolon thing is repeatedly brought up as a possible source of errors,
and I had concerns about it too at one point, but I've never seen it lead to a
single one. There are just too many other safeguards. First of all, you get
warnings on unused results for important types (for example, errors). Warning
on dropping errors covers the hazards for crypto (and I can go into more
detail here if wanted). Second, it's extremely unlikely that changing the
return type from unit to something else won't cause a type error later down
the line. For instance, returning a unit value from a function declared to
return non-unit gives you a type error.

~~~
kenko
> This style is enforced by warnings for important types, such as errors.

How do you inform the compiler that a type is important? You might not be
using the standard Result type.

~~~
pcwalton
You use an attribute, "#[must_use]".

I haven't seen code not use the standard Result type, and I don't think it's
likely. First of all, it's generic, so you aren't forced into a set of errors
or an interface that you have to conform to—there's no reason not to use it.
Second, there's strong pressure to use Result: all of the standard libraries
use that type, so you'd have to write a lot of code to convert back and forth
between your own error type and Result if you wanted to create your own for
some reason. It'd also be less convenient from an import point of view: you'd
always have to import your custom Result everywhere, whereas Result is always
available right there in the prelude. Plus, you get really convenient "try!"
macro sugar if you use Result. All of this means that you'd really have to go
out of your way to not use Result, and my experience matches this.

------
acqq
How do you format the arguments passed to println? Do you have to make new
types just to do that?

~~~
mhaymo
[http://static.rust-
lang.org/doc/master/std/fmt/index.html#fo...](http://static.rust-
lang.org/doc/master/std/fmt/index.html#formatting-parameters)

`println!("{:04d}", 1);` prints "0001". Is that what you're asking?

~~~
Someone
I think that's what he is asking. Full details at [http://static.rust-
lang.org/doc/0.10/std/fmt/index.html](http://static.rust-
lang.org/doc/0.10/std/fmt/index.html)

By the way, the Rust docs look nice, but finding this was harder than it
should be. After searching for 'println', I got at [http://static.rust-
lang.org/doc/0.10/std/index.html?search=P...](http://static.rust-
lang.org/doc/0.10/std/index.html?search=Println), which led me to
[http://static.rust-
lang.org/doc/0.10/std/macros/macro.printl...](http://static.rust-
lang.org/doc/0.10/std/macros/macro.println.html), which says as good as
nothing. The other search results ([http://static.rust-
lang.org/doc/0.10/std/io/stdio/fn.println...](http://static.rust-
lang.org/doc/0.10/std/io/stdio/fn.println.html) and [http://static.rust-
lang.org/doc/0.10/std/io/stdio/fn.println...](http://static.rust-
lang.org/doc/0.10/std/io/stdio/fn.println_args.html)) are similarly
uninformative. It would have helped if the text _" For more information, see
std::fmt and std::io::stdio"_ on that first page had hyperlinks. Now, I had to
search for _" std::fmt"_ and wade through a sea of results to get at that
first page.

~~~
steveklabnik
Macro documentation is a little bit... off at the moment. :/

