News for hackers. Stuff that matters.
Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @05:00PM (#17955146)
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)

It took hours to figure out how to implement this in rust: 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/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.

Re:Why Rust closures are somewhat hard by steveklabnik on Monday September 10, 2018 @05:07PM (#17955216)
Is 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());
Re:Why Rust closures are somewhat hard by pitaj on Monday September 10, 2018 @05:27PM (#17955359)
Yeah he's also using `if...else if` instead of a single `match`
Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @06:56PM (#17955856)
Thanks! I appreciate pointing that out.
Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @05:17PM (#17955290)
Thank 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/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.
Re:Why Rust closures are somewhat hard by steveklabnik on Monday September 10, 2018 @05:33PM (#17955405)
You're welcome! I have had a lot more time to familiarize myself with the standard library than you probably have.

That being said.. A lot of this stuff is things that already exist that you don't need to write yourself. dirname, basename, join, even s is .to_string. It's also the same as str. getenv is env().unwrap_or().

> Curious about that block in particular.

I would call https://doc.rust-lang.org/std/fs/fn.canonicalize.html and be done with it, instead of importing libc and going through all of this.

In the end, this should be like 40LOC, 50 tops, if you made use of the standard library rather than trying to re-implement it all :)

Re:Why Rust closures are somewhat hard by coldtea on Monday September 10, 2018 @05:28PM (#17955365)
>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.vars.html

https://doc.rust-lang.org/book/second-edition/ch12-05-workin...

http://siciarz.net/24-days-rust-environment-variables/

Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @05:40PM (#17955450)
FWIW, 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".

Re:Why Rust closures are somewhat hard by coldtea on Monday September 10, 2018 @05:43PM (#17955470)
That'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!".

Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @05:50PM (#17955519)
I... am not sure. On one hand, it's instructive for language designers to know what turns out to be a barrier to entry. They're often unexpected things. And what's worse, users usually run into them and then quietly disappear; you almost never get the chance to see what trips newbies up in practice. It wasn't meant as a solid criticism, but more of a cathartic "yes, this language can be really counterintuitive at times." (Rust is in good company. It's the same reason Lisp is unpopular.)

On the other hand, you're absolutely right: the hypothesis is, given two equally-experienced programmers, one of them a Rust aficionado and the other a LuaJIT maniac, the LuaJIT dev will deliver reliable and maintainable software with an order of magnitude less effort. So you're right, that remains to be seen (to put it mildly).

Looking forward to seeing how close I can get to XSV's performance. It's a great benchmark.

Re:Why Rust closures are somewhat hard by pitaj on Monday September 10, 2018 @05:39PM (#17955445)
Does `canonicalize` [1] not work in place of `realpath`?

You have a couple of helper functions, like `s` and `str` which you might consider using `to_str` and `to_string` in their place, as it's more idiomatic to use them directly rather than save characters.

And obviously you can replace `getenv` with `env::var`.

1: https://doc.rust-lang.org/std/path/struct.Path.html#method.c...

Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @06:58PM (#17955864)
Just wanted to say thanks for taking the time to give feedback. Swinging through the jungles of a new ecosystem is always fun, and guideposts like this are quite helpful.
Re:Why Rust closures are somewhat hard by withoutboats on Monday September 10, 2018 @05:07PM (#17955210)
The 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=66789a8e86e3c2fd1803adc4bec...
Re:Why Rust closures are somewhat hard by imtringued on Wednesday September 12, 2018 @04:22AM (#17966484)
I 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.
Re:Why Rust closures are somewhat hard by samnardoni on Monday September 10, 2018 @05:10PM (#17955241)
Why should Rust “just compile this” when it’s clearly bad code?
Re:Why Rust closures are somewhat hard by coldtea on Monday September 10, 2018 @05:24PM (#17955328)
It'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.
Re:Why Rust closures are somewhat hard by shawn on Monday September 10, 2018 @05:26PM (#17955353)
Well yes, if you've written 1000 lines of Rust, it's certainly trivial. Writing the first 20 lines is an <expletive> though.
Re:Why Rust closures are somewhat hard by coldtea on Monday September 10, 2018 @05:29PM (#17955375)
>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...

Re:Why Rust closures are somewhat hard by xfer on Monday September 10, 2018 @07:14PM (#17955927)
You 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?
Re:Why Rust closures are somewhat hard by leppr on Monday September 10, 2018 @05:08PM (#17955226)
For anyone new here, this is a copy pasta.