3 ms·
> Rust slaps you and demands that you clean up after yourself. This stung at first, since I’m spoiled and usually have my languages pick up after me, moreso eve
by deathanatos 7y ago
> Rust slaps you and demands that you clean up after yourself. This stung at first, since I’m spoiled and usually have my languages pick up after me, moreso even than moving from a dynamic to a statically typed language.
And yet, in the author's example, all memory handling in Rust was completely automatic. And that includes, AFAICT, no Box'd pointers¹, no ref-counting, and certainly no raw/unsafe pointers.
IME, this seems to be a common response among people coming from GC'd languages; I think the expectation is that they're going to be doing C-style memory management (manually alloc/free pairs), when the truth is that 99.9% of allocations will just happen automatically and invisible thanks to RAII.
In the end, I really think it's resources, of which memory is just one type of, that matter. Python doesn't do anything for resources, and you have to know you're allocating something (e.g., files, locks, connections, connection leases, etc.) that will require a manual .close() or with statement s.t. it gets dealloc/cleaned/released; RAII will handle this just like memory, and automatically handle it, and because of that, I find myself doing less resource management in Rust than I do in Python.
¹There are conditions in which I would argue that some uses of Box are "automatic", depending on the reason it's pulled in. E.g., I've used it to reduce the size of an enum in the common case, but still allow it to store a heavy structure in the rare case. The handling of the Box itself is essentially still automatic.
- piinbinary 7y ago> Python doesn't do anything for resources The "with" blocks can help with certain kinds of resources, for example: with open('foo.txt', 'r') as inf: data = inf.readlines()
- nerdponx 7y agoI think the point was that Python has no general tools to resource management, even if it provided ad-hoc access to the filesystem and threads.
- jhayward 7y agoI would claim that Python's context protocol [1] is exactly a general tool for resource management. [1] https://docs.python.org/3/reference/datamodel.html#with-statement-context-managers https://docs.python.org/3/reference/datamodel.html#with-stat...
- deathanatos 7y agoThat statement was too vague, you're right. I meant that it doesn't automatically do anything. I'm aware of with (it's mentioned in the post you're responding to ;) ). The thing about `with` (and similar constructs in other languages) is that you must remember to use them at every single site of use. If you forget, while the resource usually gets cleaned up when the language's GC decides to finalize the object, it's non-deterministic and might be too late. The consequences of forgetting are significantly better vs. C, as the GC will usually get to it in time, but that's not something I'd like to depend on. But the amount of "effort" and possibilities for mistakes is, generally, the same as C (in number, not severity): each resource allocation and each resource free requires the programmer's attention — excepting the specific, and I grant, quite common, case of memory, which one can say the GC will handle for you. RAII makes it s.t. you don't have to remember except in the (hopefully rare) case of implementing a new resource, and you can treat variables holding resources at sites-of-use like any other variable. For your specific example, if you can do w/o the list of lines (which I find is often possible) Python's actually got a somewhat nicer construct w/ Path: pathlib.Path('foo.txt').read_text() This only reads the data into a single string, whereas yours is a list of strings. Slight different, but usually it is acceptable. But it moves the required `with` into Path's helper method, so then it's harder for you to forget it. But this is a specific case (reading all data in a file), and doesn't generalize.
- piinbinary 7y agoGood point. There is a big gulf between "can do for you" and "automatically does for you." I really appreciate the fact that Rust does it automatically (and that it's not easy to turn that automatic management off).
- nerdponx 7y agoFWIW many objects in Python (including files) also close themselves when they are deleted (either explicitly or implicitly when their reference count drops to 0). Usage of "with" for files is more of a "just in case" idiom than a necessity.