Why are you using D instead of Rust?
jfondren
julian.fondren at gmail.com
Sat Oct 23 07:53:52 UTC 2021
On Saturday, 23 October 2021 at 04:25:21 UTC, Dr Machine Code
wrote:
> Just would like to know you all opinions
I wrote the following code and was very pleased with it until I
thought of showing it to a coworker who didn't know Rust. At that
moment I was struck by the very low ratio of "does what the
application needs doing"/"does what Rust needs doing", and the
feeling of pride turned to embarrassment. I didn't want to have
to explain why all the 'extra' code was needed, or what it did.
```rust
#[link(name = "crypt")]
extern {
fn crypt(key: *const c_char, salt: *const c_char) -> *c_char;
}
fn encrypts_to(key: &CString, salt: &CString, hash: &CString) ->
bool {
unsafe {
let ret = crypt(key.as_ptr(), salt.as_ptr());
if ret == std::ptr::null() { return false }
hash.as_c_str() == CStr::from_ptr(ret)
}
}
// ...
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encrypts_to() {
assert!(encrypts_to(
&CString::new("password").unwrap(),
&CString::new("$1$YzTLPnhJ$").unwrap(),
&CString::new("$1$YzTLPnhJ$OZoHkjAYlIgCmOKQi.PXn.").unwrap(),
));
assert!(encrypts_to(
&CString::new("monkey").unwrap(),
&CString::new("lh").unwrap(),
&CString::new("lhBnWgIh1qed6").unwrap(),
));
}
}
```
the d equivalent:
```d
/+ dub.sdl:
libs "crypt"
+/
extern (C) char* crypt(const(char)* key, const(char)* salt)
@system;
bool encryptsTo(const(char)* key, const(char)* salt, const(char)*
hash) @trusted {
import std.string : fromStringz;
return hash.fromStringz == crypt(key, salt).fromStringz;
}
unittest {
assert(encryptsTo("password", "$1$YzTLPnhJ$",
"$1$YzTLPnhJ$OZoHkjAYlIgCmOKQi.PXn."));
assert(encryptsTo("monkey", "lh", "lhBnWgIh1qed6"));
assert(encryptsTo("badsalt", "$$$", null));
}
```
This, long compile times, and losing sanity points over
async/await servers is basically it for my objections to Rust at
the time I gave d another look. Nowadays there would be more
features that I'd miss from d, like better scripting, superior
ctfe, generics, static reflection, and even code generation: Rust
has a more syntax-abusive macro system but there's a lot of uses
of it out there that are just "specialize this code over this
list of types". And there's code like
https://doc.rust-lang.org/stable/std/default/trait.Default.html#impl-Default-52
I think d/rust competition is pretty difficult atm. as d has a
bunch of newbie hazards like betterC ("wow, this hello world is
much faster! ... I can't use non-template functions from the
standard library?") and ranges ("how do I get an array out of a
MapResult?"), while Rust's relative noisiness as a language is
balanced by the compiler's willingness to nearly write the noise
for you with its error messages. Rust is mainly a lot readier in
documentation to *explain* its WTFs, vs. d where something just
seems odd (uncharitably: broken and misguided) for months until
you run across a Steven Schveighoffer post about it.
But if you can get past the newbie experience, man, just look at
the code.
More information about the Digitalmars-d
mailing list