104 points · by ingve · 9y ago · 69 comments
But in reality it is overly conservative and you have to expend effort just to get things to work.
When that happens it feels a lot more like fighting a war, a waste of resources that ideally would have been avoided.
Of course this may improve in the future, but right now I would leave the rose-tinted glasses in the drawer.
Is the borrow checker painful to grasp at first? A little. But the safety and assurance it gives you is immeasurable. Write enough Rust, and before long, you'll know when your code won't compile as written.
Plenty of people have fought the type system in Java. Happens all the time with Generics and Type eraser. How many projects have work around for that Scala, Flink, etc...
And it is a fight with the borrow checker because there a plenty valid and correct code fragments that the borrow checker rejects.
If you really don't want a hassle, then maybe you want to program in a garbage-collected language instead. But then you're potentially giving up some performance and/or memory usage savings.
And sometimes your friends are wrong because you know things they want, so you do what you were doing anyway.
I've not yet had the pleasure of using Rust, but you've described my experience of recapitulating ML-style functional programming in terms of the C# type system.
See also threads like this: https://internals.rust-lang.org/t/accepting-nested-method-ca...
As it was, I felt as though I were manipulating a child in order to get it to do what I wanted.
But yeah, we should improve this.
{
let mut x = 1;
let y = &x;
do_stuff_with(y);
x += 1 // not allowed right now because y is still active
// no use of y here. possible uses of x
}
The lexical scope of y is the entire block (well, starting at the declaration of y). The actual scope of liveness ends after do_stuff_with -- y is no longer used at this point and x could be mutated. Right now you have to introduce an explicit scope block to make this work, but it's not a fundamental restriction in the language design. let s = String::from("hello");
let s = &s[..];
Here, the second s refers to the first, and if it were go to out of scope right away, would be dangling. let s = String::from("hello");
{
let s = &s[..3];
// here, s is "hell"
}
// here, it's the full string again
This falls out of the usual scoping rules, basically. You can think of any variable introducing a new scope where it's valid; if you don't close it yourself, it's closed at the end of the enclosing scope.Why do I need to manually create a scope when the compiler can see that variable is never used again afterwards?
In Jill.name().first_name(). Why can't the compiler extend the lifetime of the return value until the end of the statement?
Having a borrow check to ensure memory safety is good. But those are just unnecessary and pointless. These are not pitfalls in user code, but pitfalls in language design
If you look at the rest of this thread, you'll see that the compiler _can't_ currently see that; this is the whole issue.
1. For a given school, return all the students
2. For a given school, return all the classes
In the last data model given (the correct one), the above 2 things can't be done (I think) without doing something funky like enumerating all the Enrollments and finding unique students/classes. In order to modify the data model to provide the needed information, wouldn't you run into the same borrow checker problems?
Edit: To clarify, I am a Rust neophyte and have only begun to scratch the surface of the language.
Edit2: Also, "There is no reason a person should know which class they are in, nor should a class know which people are enrolled in it. "
I know what the author is trying to say but when you read that at face value it comes across as a little humorous.
It could, but it doesn't yet. This kind of analysis needs porting borrowck to MIR, which still has to happen. It's too complicated given the current way it works.
Though it is a matter of explicit over implicit too. If `name()` had a destructor you may want it to be explicitly tied to a scope by dint of being declared in one instead of floating around in a magical hidden variable that's tied to the scope of a different one. So there are reasons for why it may never be made to work that way.
I'm not sure I see what the problem is with this. It seems like it is very intuitive behaviour in this case and exactly what you would expect.
This is similar to vvanders' and steve's comments in https://news.ycombinator.com/item?id=13413084 ; such things lead to a more complicated mental model even if on the surface it looks simpler.
One problem is that the user would have no way of naming the thing that the lifetime is attached to. Diagnostic messages when trying to hand off 'name' to other code could be perplexing, as the delta between broken code and working code would be larger.
28 | let name = jill.name().first_name();
| ----------- value returned here
28 | let name = jill.name().first_name();
| ^^^^^^^^^^^^^ leaves scope before use,
| and is unavailable hereAlso replacing a O(1) chasing a pointer into a O(n) array search