4 ms·
It is decidedly not a language to get things done. I say this as someone who put himself in the unfortunate position of challenging BurntSushi (author of ripgre
by shawn 8y ago
It is decidedly not a language to get things done. I say this as someone who put himself in the unfortunate position of challenging BurntSushi (author of ripgrep) to a coding duel, bragging that I could get LuaJIT to match his XSV performance (https://github.com/BurntSushi/xsv https://github.com/BurntSushi/xsv)
It took hours to figure out how to implement this in rust: https://github.com/sctb/lumen/blob/master/bin/lumen https://github.com/sctb/lumen/blob/master/bin/lumen
If you want to see an "expert" programmer's attempt at making sense of the language, here you go: https://gist.github.com/shawwn/4d7a89188cac72f3e591f47c84d3ca49 https://gist.github.com/shawwn/4d7a89188cac72f3e591f47c84d3c...
Now that it's done though, I see why people fall in love with Rust. It's really satisfying seeing Lumen's startup time drop from 200ms to 100ms.
But yes, like -- dude, at one point I just wanted to write a for loop like this, to grab some environment vars:
let mut lumen_host = "luajit";
let mut lua_path = "";
let mut node_path = "";
for (name, value) in env::vars() {
if name == "LUMEN_HOST" {
lumen_host = &value;
} else if name == "LUA_PATH" {
lua_path = &value;
} else if name == "NODE_PATH" {
node_path = &value;
}
}
println!("{} {} {} {}", bin, lumen_host, lua_path, node_path);
Good luck with that! I'm sure there's a way to coax Rust into please-allow-me-to-just-compile-this, but the borrow checker is a cruel taskmaster. Whatever the syntax is, that's not it, and spending literally an hour trying to figure out how to store an environment variable into a separate variable doesn't feel very productive.
That said, Cargo is amazing. It's truly a wonderful, delightful piece of software. Cargo crates plus a dynamic language seems like a recipe for success.
- withoutboats 8y agoThe difference between &str and String is one of my least favorite parts of Rust, but I made your code compile with some pretty trivial changes: https://play.rust-lang.org/?gist=66789a8e86e3c2fd1803adc4bec89957&version=stable&mode=debug&edition=2015 https://play.rust-lang.org/?gist=66789a8e86e3c2fd1803adc4bec...
- steveklabnik 8y agoIs there a reason you looped through env::vars() rather than calling env::var on each key you wanted? That'd be much more straightforward: let lumen_host = env::var("LUMEN_HOST").unwrap_or(String::from("luajit")); let lua_path = env::var("LUA_PATH").unwrap_or(String::new()); let node_path = env::var("NODE_PATH").unwrap_or(String::new());
- shawn 8y agoThank you. I spent ... more time than I'm comfortable admitting trying to figure out how to do that. Out of curiosity, did you spot any other antipatterns? I'm sure there are a bunch: https://gist.github.com/shawwn/4d7a89188cac72f3e591f47c84d3ca49 https://gist.github.com/shawwn/4d7a89188cac72f3e591f47c84d3c... I poo poo the language a lot, but it's satisfying to get the hang of it. fn realpath(path: &str) -> &str { unsafe { return CStr::from_ptr( libc::realpath( path.as_ptr() as *const i8, 0 as *mut i8 )).to_str().unwrap(); } } Curious about that block in particular. I have no idea what I'm doing, so it'd be somewhat shocking if that were idiomatic.
- coldtea 8y ago>Thank you. I spent ... more time than I'm comfortable admitting trying to figure out how to do that. The question is why. The relevant examples are everywhere: https://doc.rust-lang.org/std/env/fn.var.html https://doc.rust-lang.org/std/env/fn.var.html https://doc.rust-lang.org/std/env/fn.vars.html https://doc.rust-lang.org/std/env/fn.vars.html https://doc.rust-lang.org/book/second-edition/ch12-05-working-with-environment-variables.html https://doc.rust-lang.org/book/second-edition/ch12-05-workin... http://siciarz.net/24-days-rust-environment-variables/ http://siciarz.net/24-days-rust-environment-variables/
- shawn 8y agoFWIW, those four links are already purple for me. The reason it's difficult is that it simultaneously introduces the concepts of optionals, unwrapping, string processing, variable assignment, and lifetime management. It's not like it's impossible to figure out. I was only saying the workload was similar to "baby's first vi session".
- coldtea 8y agoThat's understandable, but isn't it premature to judge Rust based on such a session? That would be like criticizing vi based on "baby's first vi session", because "couldn't even exit the editor!".
- leppr 8y agoFor anyone new here, this is a copy pasta.
- samnardoni 8y agoWhy should Rust “just compile this” when it’s clearly bad code?
- coldtea 8y agoIt's not hard at all to get the above code to work, and I've only tried Rust for a couple of sub-1000-line weekend projects.
- shawn 8y agoWell yes, if you've written 1000 lines of Rust, it's certainly trivial. Writing the first 20 lines is an <expletive> though.
- coldtea 8y ago>Writing the first 20 lines is an <expletive> though. Criticizing a language by how easy it is to write the "first 20 lines" doesn't sound really fare...
- xfer 8y agoYou don't think beginner's difficulty to use a language is worth criticizing considering a number of bike-shedding changes that are done to the language to make it easy for beginners?
- imtringued 8y agoI have no rust experience but this looks like this would crash in C++ or C at runtime. You're taking a reference to a string that will be deleted in the next iteration or when the loop completes. You have to make a copy of the strings if you want to keep the code as is.