4 ms·
My reference was dropped, why is the compiler complaining about multiple borrow
- jheriko 3y agolaughs in "never needed any of these weird mechanisms to manage my own memory"
- recursive 3y agoClassic blub programmer looking up. https://wiki.c2.com/?BlubParadox https://wiki.c2.com/?BlubParadox
- ColonelPhantom 3y agoI mean, when coding memory management by hand in C or C++ (although C++ of course has things like RAII and smart pointers which simplify life a lot), you do need to have similar mechanisms within your own head. Otherwise you'll get all sorts of fun stuff like pointer aliasing, use-after-free, or what-have-you.
- oconnor663 3y agoOf course not, these mechanisms are so that other people can manage your memory :)
- Tyr42 3y agoWhat, you never deallocated the bit of memory holding where you put your keys before collecting the keys?
- mtlmtlmtlmtl 3y agoDon't forget to zero the memory first. If you don't, the keys may still be there depending on the allocator. Also, be careful about how you zero it, so the compiler doesn't optimise it out.
- Arnavion 3y agoMinor point: `let padding = &mut bytes[..index]; padding[index]` is an out-of-bounds index and will panic even if the borrow checker accepted it. You probably wanted `let padding = &mut bytes[..=index];`
- diarrhea 3y agoThank you. For some reason, I’ve always had immense trouble with index bound arithmetic (questions like: is this inclusive? Exclusive? Is ..0 a single element or none? What about ..=0?). I have to actively think about these as I do when telling East from West. North versus South is simply intuitive, no conscious thought needed. Weird effect. Glad to see I wasn’t wrong in stumbling over this particular one.
- oneshtein 3y agoFirst example is completely broken. rust:nietz 1:0
- Animats 3y agoRight. A local reference is escaping the loop scope at the "return". This is safe but weird, and the current borrow checker isn't smart enough to confirm that it's safe. The fancier "Polonius" borrow checker in development is supposed to be less restrictive. The code example is Rust written as if it were classic C. In Rust, that example would usually be written like this: /// Find leading zeros in an array of bytes. fn find_leading_0s(bytes: &mut [u8]) -> Option<&mut [u8]> { if let Some(pos) = bytes.iter().position(|v| *v != 0 ) { // search Some(&mut bytes[..pos]) // find } else { None // no find } } Rust isn't a fully functional language, but the functional features play well with the lifetime system and are optimized well. (Rust playground example: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=8013c9e08e14db8c7b0d7764c01d1674 https://play.rust-lang.org/?version=stable&mode=debug&editio...)
- angiosperm 3y agoA simple "--make-borrow-violations-into-warnings" flag (that for the sake of the panicky would work only on debug builds) would let you get on with your work, and come back before pushing to a shared repository to settle up. Probably need to release it as a patch so as not to need approval of the purity gatekeepers. People using it get an immediate boost in productivity as it enables exploration with no ultimate reduction in safety.
- returningfory2 3y agoDo you think there should also be a “--make-type-violations-into-warnings’ flag?
- estebank 3y agoI think there would be value of such an option, as long as it would be completely forbidden to publish to crates.io with code that only works with those flags. It could work by replacing any function with type errors (including those caused by type definitions) to panic on use, but otherwise still generate a binary. For type errors it is less useful than for lifetime errors because usually the problems of the former are very local while for the later it they are not.
- kramerger 3y agoCurrently fighting the borrow checker. My project has not moved much this last week due to an issue I just can't understand. Have rewritten the offending code 4 times but it always fail to compile somehow. At this point, I am only staying with Rust because of the ergonomics of enum and iterators. The memory safety stuff is 90% writing safe code then fighting the compiler to agree with you. :(
- deleted 3y ago[deleted]
- chrismorgan 3y ago> The memory safety stuff is 90% writing safe code then fighting the compiler to agree with you. This isn’t my experience, from a decade of occasionally watching and helping others learn Rust, personally and professionally. Rather, it’s normally writing code that you believe is safe, wrestling with the compiler and finally realising that it was right all along, or giving up before reaching this stage but it was still. There are certainly some cases where this isn’t the case, and this article talks about the most common and most notorious one, but seriously, the compiler is normally right. Somewhere along the way, things click and your intuitions subsequently almost always match the compiler’s, and you have a much happier time of it, seldom needing to wrestle. And your coding in other languages tends to improve, or at least focus on ownership more in ways that tend to make later maintenance easier.
- kstrauser 3y agoMe, writing a thing in Python: La, la-la, la-la. Me, writing it in C: Grumble, grumble, la-la-la. Me, writing it in Rust: I HATE THIS THING WHY DO YOU oh hey I'd been thinking about this all wrong. I go through that cycle all the freaking time. At first, I'm irked that Rust is being such a pain in the neck. Then I give in and do things the way it wants me to, and realize that it's genuinely much better that way, even if I then take that approach back to Python. At the least, now I'm more aware of when I'm counting on the garbage collector to atone for my sins.
- 4death4 3y ago