Hacker News new | past | comments | ask | show | jobs | submit login

I have a question for people who've advanced ahead of me into rust.

With the C and the kind of C++ I write I have some kind of idea what the assembly language that will be generated by the code I'm writing looks like. I have some idea how the data is going to be laid out in memory. You can think of C and good C++ code as a more convenient way of writing the machine instructions that will execute on your cpu, well at least kinda. But you really can't do this with say, haskell or clojure (he said aware he knows little about the former and even less about the latter).

How is rust? Can you write some rust code knowing this datastructure will be laid out like so, knowing this function will likely not thrash cache too badly and so on? Inline asm? If you had the crazy idea of writing the "platinum" linker in rust in an attempt to outperform the (written in c++) gold linker is that doomed before you start. Nothing to choose for efficiency between these languages?

Oh and how's the debugger? Same as gdb on c++?




Being able to have an idea of the assembly that will be generated from Rust code is certainly a motivating goal (I believe that pcwalton has previously indicated his ability to divine as much), though I personally have no experience with bare assembly so I cannot attest to it myself.

You can certainly guarantee data layout (such is required for C interop, for which Rust has very nice support), though by default Rust will e.g. reorder a struct's fields in order to optimize its representation (taking into account padding and such) unless you explicitly ask it not to.

Rust also tries to favor explicitness where it can. For example, even though many people have requested C#-style properties where `foo.bar = baz` can implicitly call a setter function, Rust has rejected this because the runtime behavior of a function call is too different from the runtime behavior of a field lookup. As another example, anything that looks like `foo.bar()` will always be a method call, and `foo.bar` (without direct parentheses) will always be a field lookup, so if you want to call a function pointer stored in a struct field you need to write `(foo.bar)()`.

There are also places where Rust is more explicit than C++. For example, a C++ function `void bar(int& foo)` can be called like `bar(6)`, but the analogous Rust function `fn bar(foo: &i32)` must be called like `bar(&6)`.

However, Rust also has places where it is less explicit than C++. For example, whereas C++ has both `bar.foo()` for direct method calls and `bar->foo()` for method calls that are invoked through a deref, Rust has only `bar.foo()` which has the capability to dereference `bar` if necessary. So it would be incorrect to say that Rust is ultimately more or less explicit than C++... it just has a different mix of explicit and implicit operations.

As to the rest, Rust does indeed have inline asm, and I hear that Rust produces DWARF debuginfo which allows it to be debugged via gdb (though it has to pretend to be C++ in order to play nicely with existing tooling, which can make it awkward sometimes).


Nice answer, thanks!




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: