How low-level is Rust?


#1

In Rust can you differentiate between different types of memory addresses just like you can in C++?

Is there something C can do that Rust can’t?


#2

What do you mean by “different types” (of memory addresses) exactly?

One thing that comes to mind that C can do that Rust can’t (currently) is alloca (e.g. allocating a dynamically sized array on the stack) but I never needed that in Rust…

Also, C compilers exist for more platforms than Rust/LLVM, e.g. m68k or certain microcontrollers…


#3

Honestly I don’t know as one of my friends told me that in Rust he thinks that you can’t do that whereas in C++ you can.

In Rust is this feature “allocating a dynamically sized array on the stack” currently being worked on at the moment?


#4

It is being worked on: https://github.com/rust-lang/rfcs/pull/1909

But keep in mind:

this particular feature of C99 is debatable, it has certain advantages (cache locality & bypassing malloc) but it also has disadvantages (easy to blow-up the stack, stumps a number of optimizations, may turn static offsets into dynamic offsets, …)


Btw, it seems like you’re trying to decide whether you should learn Rust, is that the case? :slight_smile:

I think Rust is worth learning and using, and it’s my favorite language, I use it at my job and in my spare time, so I would definitely recommend it (over C/C++). It still makes sense to have knowledge of C/C++ in general but I wouldn’t recommend using C/C++ for new projects (unless there are non-technical reasons that force you to). But it probably makes sense to learn C before learning Rust…


#6

Oh ok that is awesome :slight_smile:

Yes you are correct I am deciding whether I should learn either C, C++ or Rust and I don’t want to waste time learning and investing my time with one language, only to find out that this language can’t do what the other language can and should have learn’t that language etc.

Have you learn’t and used C and C++ before? If so then in your opinion which language do YOU personally prefer using?

Why would you say to learn C before learning Rust?


#7

IMHO it is advantageous to learn many languages, just like it is advantageous to learn many disciplines of software development (not just programming).

If you want to get Into the technicalities of whether there are things you can do in one language vs another, the first thing I would think to check is support for inline assembly, or at least FFI. Rust has a very good FFI story, so it can literally call into any code, even if it is written in C/C++. There is some basic support for inline assembly in nightly, with some attempts at improvements:

Given these options, it’s possible to write anything in Rust. Even an entire operating system like redox.


#8

What about for robotics and for microcontrollers? Can Rust be used for that as well?


#9

It depends which micro controller you are targeting. A good place to start would be the embedded working group: https://github.com/rust-lang-nursery/embedded-wg


#10

Would Rust eventually support more and more micro controllers?

May I know which language is the most lower level language, C, C++ or Rust?


#11

Eventually, yes rust intends to support more embedded devices.

I already answered your other question. How can I help provide more clarity?


#12

I just want to know now which language is lower level language as mentioned above?


#13

Because all three support some variation of both FFI and inline assembly, that makes each language effectively equivalent on the scale of “how low level is your language.” They are all as low level as a language can get without being pure machine code binary blobs.

(This is oversimplifying the situation a bit. I’m just trying to answer your question pragmatically.)


#14

What about without the use of FFI and inline Assembly which one would be lower level then?

Sorry I am asking a lot of questions I am just curious that is all.


#15

I personally think Rust us the “better” language compared to C/C++, it pays more attention to correctness and memory safety (less “gotchas” and footguns, guaranteed memory safety), better programmer ergonomics (helpful error messages, great docs), and has waaaay better abstractions (praise the Typesystem! And lovely iterators)
Unfortunately, it is still a young language, so it cannot (yet) match C/C++ on ecosystem size and device support. (Although people do some crazy stuff cross-compiling Rust>C>obscure-target, search for “mrustc” and “rust on esp8266”)

If you learn Rust for microcontrollers, you will curse having to write so many libraries yourself,
If you learn C for microcontrollers, you will curse yourself “Why is this crashing!!?!!”

Both languages are equally valuable to learn, and understanding one will help you understand the other.


#16

Thanks for that my man :slight_smile:

Without the use of FFI and inline Assembly which one would be lower level then? Rust, C++ or C??


#17

They are equally low level for all intents and purposes; both languages can run without any form of runtime, handle raw pointers to memory locations (needs unsafe in Rust, because no guarantees), and compile down to native machine code.

People write operating systems (see redox), kernels, webbrowsers (Servo, Firefox) and microcontrollers (look at japaric’s work) in both of them.
(I’d add more links, but I’m on mobile)

The next lower level for both of them is “pure assembly”, and then only “ones and zeros”.


#18

Thanks for clearing that up my friend.

What is “unsafe” exactly, is it a function that voids all Rust’s built in safety measures so you can write unsafe code freely?


#19

Why are you excluding FFI, but concidering dynamic stack allocation as important feature? FFI is a way to call SW written in another language, and export code for another language, so this is actually essential feature (without it only C would be useable). DSA is feature which probably almost noone needs for now, and if you would fall into case when you would really need it, you would really don’t care about language.
Unsafe is feature which allows you to violate rust safety (eg. reinterpret binary data as another type).


#20

It’s almost they exact opposite of that, unsafe means “dear compiler, I know that this next bit is not normally allowed and you are trying to prevent me from shooting myself in the foot, but what I am doing here is actually safe, I promise! I just can’t prove it to you. Please trust me, I know what I’m doing

It suspends any and all quite a few compiler checks for that block, so you have more freedom, but you still have to uphold all guarantees yourself (now without compiler assistance).
This may sound hard, but it is basically exactly what every single line of C has been like for the past four decades…

Edit: not all compiler checks, as repeatedly pointed out, thanks everyone!


#21

Why would C be useable without this feature compared to Rust?