In a struct, you typically want only owned data types. If you’re passing something as a parameter to a function, you typically want only references.
In a sense, that pushes your program to be very tree-shaped. Your data is initialized and held at the root of the tree (or a sub-tree) and only temporarily passed down as a reference.
In my experience, following this rule gets you there for 80% of programs. Only really more complex programs or libraries may need to deal with lifetimes and the various cop-out mechanisms.
If you’re hurting your brain, that’s probably because you’re experienced in other languages and have design patterns in mind that don’t work with this model.
I am in the process of learning rust by creating software for my own use.
While the borrow checker feels like “fighting the compiler” at times (it took me a week before it started to land, and i have used my share of programming languages!), you will learn.
I compare this to the C world where you can have quite complex constructs with pointers and references. That used to be hard as hell as well. Until it clicks.
The only difference is that in C land, your software will segfault during runtime. In Rust your code will complain during compilation.
Rust without borrow checker is just a bad version of C in my opinion. Rust with borrow checker actually brings value.
In rust it is still possible to make circular references and cause memory leaks but it is very less likely.
So: i understand where you are coming from but give it a chance to land. I am glad I did.
The only thing which is bothering me at times is that the Rust community can come across as highly unfriendly/intolerant at times (comparable to the scala community, which in effect killed the adoption of scala).
The response to your comment should not have been burning it to the ground but to ask why the borrow checker does not click for you.
I can highly recommend the series “lets get rusty” on YT from Bogdan. He is explaining the main constructs in very easy to follow instructions videos.
What’s the point of using Rust, if you don’t want to think and program in Rust? If you seriously don’t want to learn and deal with safe code and think every step of it in advance before compilation, then Rust is the wrong language for you. Either use a low level language like C and Zig, which gives you control over the system, but does not have a borrow checker. Or use a language with a runtime check that does this automatically for you without a borrow checker, like Go in example.
There does exist a crate that allows you to turn it off. Unfortunately the compiler will still compiler your code assuming the same exclusive access rules imposed by the borrow checker, so any time you break the borrow checker rules you’re basically guaranteed to segfault.
The rust compiler always assumes mutable pointers are exclusive. This is the crux of your issue with the borrow checker. If you don’t make this assumption, then you can’t automatically drop variables when they fall out of scope, thus the programmer would have to manually allocate memory.
You may prefer even then to allocate memory yourself, but if I was you I would just trust the rust compiler in its assumption that it can optimize programs much better when all pointers are exclusive, and learn to program in a compliant manner
Especially because the borrow checker is the point, the added value, of rust. With it it can ensure compile time memory safety, without it it is just another programming language.
His comment should be read as “I don’t get the borrow checker”.
Response of the Rust community: “lets vote him to oblivion”.
Response should be: what part of it is not clear, hand him reference material etc.
This was the same with scala (which has its userbase cut in 1/10th of what it is). Scala is now almost extinct.
I would hate for Rust to become the next Scala because it offers so much nice features (for me that is native binaries) among others.
I’ve had my share of response to honest questions which resulted in comments that I should be using language X, or that I have some nice term wrong (which does not relate to the problem I was experiencing), I am asking the wrong question and so on.
Luckily there also were helpful comments, which kept me in the Rust ecosystem.
The other behaviour is not productive and just makes Rust look like a niche, elite and too hard language.
Be nice to each other. It will only make Rust and its community stronger.
There are dozens of tutorials and books to get started with Rust. At this point no one’s writing this kind of comment in good faith. Especially in a community dedicated to Rust.
Yeah… I’ve written about this sensitivity before on Lemmy. :) I honestly think this platform is best for memes and agreeing with popular opinions. And that’s fine I guess.
I’m not sure what you’re expecting people to reply with when commenting that in a Rust community. People here tend to use and like Rust, so it’s a bit odd to make sarcastic remarks here about one of Rust’s biggest features and expect people to react well.
I don’t know, I guess I thought a rust community could have different opinions about Rust. I’m a bit older and I’m OK with people posting negative stuff about things I like without downvoting them. :)
I don’t have that need to push down opinions I don’t agree with. I guess I just don’t see myself as needing to do that. I don’t feel angered by that sort of stuff.
But you didn’t give your opinion. You made a low effort snarky comment about the most defining feature of the language. And then, you cry people are sensitive…
It’s fascinating to see you think that… I mean, it’s also a form of entertainment to realize that people like yourself overreact to things and have no idea that what they think is wrong. Anyway, have a good one. :)
unsafe does not disable the borrow checker. It does however give you abilities to circumvent it, namely by letting you dereference pointers, which are not subject to the borrow checker.
You can also selectively circumvent it without dipping into unsafe, by wrapping a type in an Rc or Arc (“reference-counting” and “atomic reference-counting”). This will allow you to handle an object largely like you might expect from garbage-collected languages. (Reference-counting is not able to free circular dependencies. If you need those, then use WeakRef.)
Having said that, I would certainly not recommend constantly doing that for a beginner. It needs some time to get used to the way Rust works, especially if you’re already experienced in other languages, but when it clicks, then you stop breaking your brain.
Let us know when the borrow checker is optional so I can write it without hurting my brain.
In a struct, you typically want only owned data types. If you’re passing something as a parameter to a function, you typically want only references.
In a sense, that pushes your program to be very tree-shaped. Your data is initialized and held at the root of the tree (or a sub-tree) and only temporarily passed down as a reference.
In my experience, following this rule gets you there for 80% of programs. Only really more complex programs or libraries may need to deal with lifetimes and the various cop-out mechanisms.
If you’re hurting your brain, that’s probably because you’re experienced in other languages and have design patterns in mind that don’t work with this model.
lol just go write C instead
Maybe use zig then? It’s unsafe (i.e. no borrow checker) and really fast.
But honestly, after learning good patterns, I rarely run into borrow checker issues.
If you use it long enough, the borrow checker will change your brain. Embrace the change, it will help you.
I am in the process of learning rust by creating software for my own use.
While the borrow checker feels like “fighting the compiler” at times (it took me a week before it started to land, and i have used my share of programming languages!), you will learn.
I compare this to the C world where you can have quite complex constructs with pointers and references. That used to be hard as hell as well. Until it clicks.
The only difference is that in C land, your software will segfault during runtime. In Rust your code will complain during compilation.
Rust without borrow checker is just a bad version of C in my opinion. Rust with borrow checker actually brings value.
In rust it is still possible to make circular references and cause memory leaks but it is very less likely.
So: i understand where you are coming from but give it a chance to land. I am glad I did.
The only thing which is bothering me at times is that the Rust community can come across as highly unfriendly/intolerant at times (comparable to the scala community, which in effect killed the adoption of scala).
The response to your comment should not have been burning it to the ground but to ask why the borrow checker does not click for you.
I can highly recommend the series “lets get rusty” on YT from Bogdan. He is explaining the main constructs in very easy to follow instructions videos.
Have a look and see if it makes it work for you!
What’s the point of using Rust, if you don’t want to think and program in Rust? If you seriously don’t want to learn and deal with safe code and think every step of it in advance before compilation, then Rust is the wrong language for you. Either use a low level language like C and Zig, which gives you control over the system, but does not have a borrow checker. Or use a language with a runtime check that does this automatically for you without a borrow checker, like Go in example.
It is. You just need to change the extension to
.c
Or zig. Heard a lot about that one. Haven’t really done any serious testing though.
There does exist a crate that allows you to turn it off. Unfortunately the compiler will still compiler your code assuming the same exclusive access rules imposed by the borrow checker, so any time you break the borrow checker rules you’re basically guaranteed to segfault.
The rust compiler always assumes mutable pointers are exclusive. This is the crux of your issue with the borrow checker. If you don’t make this assumption, then you can’t automatically drop variables when they fall out of scope, thus the programmer would have to manually allocate memory.
You may prefer even then to allocate memory yourself, but if I was you I would just trust the rust compiler in its assumption that it can optimize programs much better when all pointers are exclusive, and learn to program in a compliant manner
Bait or not, I’m not sure why you’re getting such a negative reaction. People are getting too sensitive!
Btw, do sanitizers hurt your brain too?
It’s not because people are sensitive, it’s because Rust gets a lot of dumb criticism and people are tired of it.
Especially because the borrow checker is the point, the added value, of rust. With it it can ensure compile time memory safety, without it it is just another programming language.
It is just because people are sensitive. Period.
His comment should be read as “I don’t get the borrow checker”.
Response of the Rust community: “lets vote him to oblivion”.
Response should be: what part of it is not clear, hand him reference material etc.
This was the same with scala (which has its userbase cut in 1/10th of what it is). Scala is now almost extinct.
I would hate for Rust to become the next Scala because it offers so much nice features (for me that is native binaries) among others.
I’ve had my share of response to honest questions which resulted in comments that I should be using language X, or that I have some nice term wrong (which does not relate to the problem I was experiencing), I am asking the wrong question and so on.
Luckily there also were helpful comments, which kept me in the Rust ecosystem.
The other behaviour is not productive and just makes Rust look like a niche, elite and too hard language.
Be nice to each other. It will only make Rust and its community stronger.
There are dozens of tutorials and books to get started with Rust. At this point no one’s writing this kind of comment in good faith. Especially in a community dedicated to Rust.
That’s not what he wrote.
Yeah… I’ve written about this sensitivity before on Lemmy. :) I honestly think this platform is best for memes and agreeing with popular opinions. And that’s fine I guess.
I’m not sure what you’re expecting people to reply with when commenting that in a Rust community. People here tend to use and like Rust, so it’s a bit odd to make sarcastic remarks here about one of Rust’s biggest features and expect people to react well.
I don’t know, I guess I thought a rust community could have different opinions about Rust. I’m a bit older and I’m OK with people posting negative stuff about things I like without downvoting them. :)
I don’t have that need to push down opinions I don’t agree with. I guess I just don’t see myself as needing to do that. I don’t feel angered by that sort of stuff.
But you didn’t give your opinion. You made a low effort snarky comment about the most defining feature of the language. And then, you cry people are sensitive…
Nice troll
It’s fascinating to see you think that… I mean, it’s also a form of entertainment to realize that people like yourself overreact to things and have no idea that what they think is wrong. Anyway, have a good one. :)
How do you measure an overreaction over text?
Isn’t it already optional? Just put
unsafe
on everything?unsafe
does not disable the borrow checker. It does however give you abilities to circumvent it, namely by letting you dereference pointers, which are not subject to the borrow checker.You can also selectively circumvent it without dipping into
unsafe
, by wrapping a type in anRc
orArc
(“reference-counting” and “atomic reference-counting”). This will allow you to handle an object largely like you might expect from garbage-collected languages. (Reference-counting is not able to free circular dependencies. If you need those, then useWeakRef
.)Having said that, I would certainly not recommend constantly doing that for a beginner. It needs some time to get used to the way Rust works, especially if you’re already experienced in other languages, but when it clicks, then you stop breaking your brain.
OIC. I really need to start learning this language soon, or I won’t be able to understand future memes and rants.