Info
This post is auto-generated from RSS feed The Rust Programming Language Forum - Latest topics. Source: Why leaning C before Rust is not necessary
I'm going to support my claim that it is not necessary to start with learning C to appreciate Rust with examples.
Arguably the Raison d'รชtre for Rust is to provide a language that can be used in situations where C might be used but avoiding the memory misuse mistakes that are easy to make in C. That is to say the borrow checker is the unique selling point.
About the simplest example of a memory use error for our student might look like this in C:
#include <stdio.h>
int* func() {
int x = 42;
return &x; // Returns pointer to local variable
}
int main() {
int* ptr = func();
printf("Value: %d\n", *ptr); // Dangerous! Accessing expired memory
}
Modern C compilers will warn about returning a pointer to a local variable. My clang does actually build it and produce the 42 when run. A typical insidious mistake in C. It will also produce the wrong result when optimisations are on.
The Rust equivalent of that example is this:
fn func() -> &'static i32 {
let x = 42;
&x // Error! Cannot return reference to local variable
}
fn main() {
let ptr = func(); // Would attempt to store expired reference
println!("Value: {}", *ptr); // Would cause undefined behavior
}
Which looks almost exactly the same. Except of course. But now it fails to compile with an error "returns a reference to data owned by the current function".
Given these examples are so similar, my question is why would start our student off with C?
Now a suggestion. If one clings to the "start with C" approach, why not show our student "Crust". The same example in Rust but using raw pointers:
fn func() -> *const i32 {
let x = 42;
&x
}
fn main() {
let ptr = func();
unsafe {
println!("Value: {}", *ptr); // Must use unsafe block to dereference raw pointer
}
}
Looks almost exactly like the C version. Apart from that mysterious unsafe
. But the reason for that becomes apparent almost immediately.
Interestingly Rust will build and run that with no error or warning at all, happily producing the wrong result. Perhaps one could argue that C has advantage in that resect. But that C warning is only a warning, it's not required by the language, not all compilers will give a warning, and in many situations there will be no warning even in
clang or GCC.
However try to run this with cargo miri run
and we will get an error with a nice explanation of the problem. Sweet.
In order for our student to understand this one has to get into talking about local variables and the stack and hence lifetimes. No matter if we start with C or Rust. Might as well skip the C part.
Edit: Hmm... A radical suggestion... perhaps starting off Rust students with Crust and raw pointers instead of references is actually easier. It does away with those mysterious lifetime tick marks, I mean like the 'static'
, until our student is in a position to appreciate their purpose and meaning.
3 posts - 3 participants
๐ท๏ธ rust_feed