4 ms·
> and the first couple of introductory chapters should use only unsafe code (I'm only half joking on both points) It's really at least half-true. As far as I u
by acqq 10y ago
> and the first couple of introductory chapters should use only unsafe code (I'm only half joking on both points)
It's really at least half-true. As far as I understand the libraries of Rust itself can't have everything written in "safe" code.
The hard part is to win the potential programmers to use the safe code as much as they can but to understand where the limits are.
And that is the hardest thing to do as a lot of people prefer simplifications the kind of "everything must be done the X way" etc. I call that a "religious programming" approach and the programming world has it in immense amounts.
- akiselev 10y ago> As far as I understand the libraries of Rust itself can't have everything written in "safe" code. You can't express everything in safe code but wrapping an unsafe API into a safe one is relatively easy with Rust's type system. For example, typemap [1] allows you to have a unsafe hash map of types with any key type to any value through a safe wrapper that uses associated types to enforce the data structure invariants at compile time. Given a bunch of types T, for example, you can have a single safe map containing all of the mappings of Event<T> to Vec<T>, Handlers<T> to HashMap<T, FnMut(T)>, and T to &T, etc, even though transmuting random bits of memory is the definition of unsafe. [1] https://github.com/reem/rust-typemap https://github.com/reem/rust-typemap
- acqq 10y agoI've never disputed that. This duality (higher level allows safe abstraction of the low level) existed since the higher level languages exist.
- AsyncAwait 10y ago> As far as I understand the libraries of Rust itself can't have everything written in "safe" code. Extremely low-level code or code that uses FFI indeed uses unsafe, this in unavoidable, but most Rust code doesn't need unsafe at all.
- acqq 10y agoAnd what is disproved with that claim? I've made my own stats: Searching for "unsafe {" in the source files of Rust gives: Matching lines: 2499 That's how many time some block is marked with "unsafe {" Matching files: 663 That's how many files have it. Total files searched: 8404 It's quite small source base. But then I've removed the files in "test" folder. What I get is: Matching lines: 1804, Matching files: 290, Total files searched: 1302 That means that almost every fourth rs file of Rust, when the test files are ignored, has "unsafe {" in it. To quote you: "this in unavoidable." I agree, and nobody should pretend that it is avoidable. The reason people have to use C is exactly to do "hard stuff" on the "low" level you mention. Otherwise nobody would use anything but Python and equivalents. What is called "low" level is actually the efficient algorithms and the efficient numerical calculations. And of course, once you implement that, you can keep the higher levels "safe." It takes a kind of honesty to be able to admit that much. Disclaimer: I like Rust and I don't like extremists.
- AsyncAwait 10y agoI don't broadly disagree with you,I am just saying that is not as big of a thing as it may at first seem, most people aren't writing compilers or web browser engines and thus if the unsafe is hidden behind a safe abstraction in a 3rd party library, often you don't even come to contact with unsafe directly and even when you do, it's contained in a block that you can easily grep for.
- jjnoakes 10y agoHow many lines of code are inside those unsafe blocks, versus in the entire source tree? And how many of those unsafe blocks could be removed and consolidated into libraries, if they are doing similar things?
- Manishearth 10y agoThey grepped the stdlib, this unsafe is already in a library.