5 ms·
> 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
by 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.
- jjnoakes 10y agoBeing in one or more libraries already obviously doesn't preclude them from being combined together if they are doing similar things in different places...
- steveklabnik 10y ago> in the source files of Rust When deciding what should go in the standard library vs not, one qualification is "does this use a lot of unsafe?" If so, it should. This is so it gets more eyeballs on it. The compiler and standard library will have much more unsafe code than most projects for largely this reason.
- acqq 10y agoIsn't it, independently of that argument ("more eyeballs"), unavoidable to use unsafe code to efficiently implement the new algorithms, new efficient containers or efficient numerical processing? (And why is there a need to "defend" the idea that it isn't? I already pointed to my answer to that in my topmost post.) Edit: to answer ""unavoidable" is a very strong statement" from the answer of this post: how could the very presence of "unsafe" parts exactly in the code written by the most informed programmers of that language be otherwise explained? I can't imagine anything more representative than the compiler and the libraries, except for the numerical algorithms, which are typically not included in the libraries of a lot of languages.
- steveklabnik 10y ago> unavoidable It's not possible to tell, this question is far too broad. And "unavoidable" is a very strong statement. > And why is there a need to "defend" the idea that it isn't? I'm not; I'm saying that the rust-lang/rust repo is going to have a much higher percentage of unsafe than a random Rust repo. Most applications have no unsafe, most libraries have little to no unsafe. Some libraries have a lot of unsafe. That is, you chose the compiler as something intended to be representative, I don't think that's accurate.
- AsyncAwait 10y agoThe way I originally meant the "unavoidable", (in response to a comment that there's unsafe code in the Rust compiler), is that in some cases, you'll always need unsafe, since some operations are inherently unsafe, (like writing bytes directly to hardware). This due to the nature of computers, the lower level the code gets, the more likely it is that some unsafe will be needed, not because of any fault of Rust, but because of the nature of computer hardware. Therefore I meant it as in; since a compiler is relatively low-level project, some unsafe code is (most likely) unavoidable, due to the task at hand. The other poster seemed to take the unavoidable comment to mean something wider than what I was talking about.