TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Rust Ownership Rules

317 pointsby dadeabout 5 years ago

10 comments

rob74about 5 years ago
I would argue that the phrase "One of Rust's main differentiator[s] is that it provides memory safety" should really be "One of Rust's main differentiators is that it provides memory safety without using a garbage collector". Which is the actual reason for needing (amongst others) ownership rules...
评论 #22464452 未加载
评论 #22463521 未加载
评论 #22463474 未加载
评论 #22469931 未加载
评论 #22464272 未加载
quietbritishjimabout 5 years ago
I am a long time C++ user and have looked into Rust a bit but not used it in anger.<p>One thing not mentioned in the section about ownership move: unlike C++ where you can write arbitrary code in the move constructor, in Rust the footprint of the object is copied bitwise and there is no scope to customise this at all. If you have a class where this doesn&#x27;t work (e.g. because it contains pointers into its own footprint) then you either need to refactor your class for Rust (e.g. change those pointers to offsets) or disable the move trait entirely and provide a utility function for move-like creation.<p>This is a trade off: as a class writer you get less flexibility, but as a class user you get much more predictable behaviour. And it&#x27;s only possible because of the way that Rust just forgets about the original object (i.e. won&#x27;t call its destructor at the point it would have done otherwise) at the language level. If it didn&#x27;t, as in C++, you need some way to stop the original object freeing resources needed by the new object.<p>IMHO, move semantics and rvalue references in C++ are amongst the most confusing and worst designed parts of the language, so this is one of the most important benefits of Rust even before you get to the reference lifetime stuff.
评论 #22467886 未加载
评论 #22464219 未加载
评论 #22464558 未加载
评论 #22464940 未加载
_nalplyabout 5 years ago
I read somewhere that immutable and mutable borrows are perhaps alternatively understood as exclusive and shared borrows.<p>This means, if you have a &amp;mut then you have an exclusive borrow. No other borrows are possible as long as your &amp;mut lives (i. e. did not yet go out of scope). Usually this exclusive borrow is used for mutating values.<p>This dichotomy in terminology is shown when you learn about interior mutability. Here you can mutate values without having an exclusive borrow, an example is RefCell. The price you pay for this convenience is run-time checking of access. RefCell disallows mutation if some other location in your code already has asked for mutable access.
评论 #22462775 未加载
评论 #22463569 未加载
评论 #22463695 未加载
评论 #22462752 未加载
评论 #22469660 未加载
fauigerzigerkabout 5 years ago
What I like most about Rust is that I can pass references around without worrying whether the value they reference will continue to exist. This has been a constant mental burden for me in C++ and has led to some unnecessary defensive copying.<p>I&#x27;m not so sure whether Rust&#x27;s strong immutability&#x2F;exclusivity guarantees are worth the trouble though. Unexpected mutation hasn&#x27;t been a major source of bugs or mental burden for me in other languages, at least not in a single threaded context.
评论 #22463208 未加载
评论 #22467474 未加载
评论 #22464283 未加载
评论 #22463204 未加载
评论 #22466582 未加载
supermattabout 5 years ago
I am just getting into rust myself - following from that 30 min article a few days ago.<p>From my understanding, the comments in a few of the examples are misleading. Author is stating things like &quot;&#x2F;&#x2F; mutable borrow occured&quot; where there is no mutable borrow occuring. My understanding is that there is an implicit mutable borrow where the methods are being called (e.g. `original_owner.push(&#x27;.&#x27;);`) which is raising the errors<p>Can anyone with more experience confirm&#x2F;deny that this is the case, as I want to be sure I am not misunderstanding this
评论 #22463095 未加载
评论 #22463060 未加载
ashabout 5 years ago
Lobster language offers interesting lightweight alternative to strict ownership model of Rust:<p><a href="http:&#x2F;&#x2F;aardappel.github.io&#x2F;lobster&#x2F;memory_management.html" rel="nofollow">http:&#x2F;&#x2F;aardappel.github.io&#x2F;lobster&#x2F;memory_management.html</a>
评论 #22471193 未加载
frankmcsherryabout 5 years ago
In the event that it helps anyone, the following is how I think of Rust ownership stuff:<p>1. Ownership-based memory management is statically elided reference counting.<p>2. Borrowing (shared and mutable) is statically elided reader-writer locks.
评论 #22464390 未加载
评论 #22464020 未加载
rk06about 5 years ago
They look pretty simple and intuitive.<p>From what I heard about rust&#x27;s learning curve, I expected complex rules and corner cases.
评论 #22466741 未加载
clktmrabout 5 years ago
Some commenters mention Rust has compile-time data-race checking. Is this correct? From what I did understand it will only enforce a mutex to be locked before you can access specific data. However if there is no mutex, data-races won&#x27;t be detected at compile time?
评论 #22464760 未加载
评论 #22464767 未加载
animalnewbieabout 5 years ago
Somewhat unrelated but does somebody have the 30 minutes guide to reading rust from 5 days back?
评论 #22462559 未加载
评论 #22462744 未加载
评论 #22462582 未加载