Live data from Hacker News

Rust's language ergonomics initiative

blog.rust-lang.org

1–10 of 295 posts

Re: Rust's language ergonomics initiative

#2
I especially like this approach:

> Often, the heart of the matter is the question of what to make implicit. In the rest of this post, I’ll present a basic framework for thinking about this question, and then apply that framework to three areas of Rust […]

What's proposed here is a universally good way to think about what to make implicit. The proposed changes to Rust are just some applications of this.

Re: Rust's language ergonomics initiative

#4

I still can't get my head around rust. While all those features definitely make sense, I find it very confusing sometimes. Is there something like rust for c++ programmers?

https://github.com/nrc/r4cppp exists, I haven't read it though. Or at least, not in a very long time.

Re: Rust's language ergonomics initiative

#5
"Idea: implied bounds" sounds like a very interesting idea. It is a pain copying the bounds as author mentions. I also have worked with library code that does not consistently use trait bounds and it can lead to very confusing errors.

The thing that keeps getting me now is there are so many types moving around with generics and traits. It would be nice if it were easier for something to be object safe and/or Any was more powerful. My solution as with many things is to route around it but it is frustrating at times.

Re: Rust's language ergonomics initiative

#6

"Idea: implied bounds" sounds like a very interesting idea. It is a pain copying the bounds as author mentions. I also have worked with library code that does not consistently use trait bounds and it can lead to very confusing errors. The thing that keeps getting me now is there are so many types moving around with generics and traits. It would be nice if it were easier for something to be object safe and/or Any was…

> Any was more powerful

https://github.com/rust-lang/rfcs/pull/1849 is relevant to your interests; however, as the comments say there, associated type constructors would be needed before it could possibly be used with Any.

Re: Rust's language ergonomics initiative

#7
A challenge I've had with Rust lately is factoring initialization code into separate functions. Because of stack-based allocation it has to stay in the main function. For example:

    pub fn do_many(iter: &mut Iterator) {
      let mut job_id = None;
      let job_id_env = env::var("MYAPP_JOB_ID");
      let mut log = if let Ok(val) = job_id_env {
        write_pid_file(&val);
        job_id = Some(val.clone());
        let home = env::var("HOME").expect("HOME must be set");
        let path = format!("{}/log/myapp-{}.log", home, val);
        let path = Path::new(&path);
        match File::create(&path) {
          Ok(mut f) => Box::new(f) as Box,
          Err(e) => {
            if format!("{}", e) == "No such file or directory (os error 2)" {
              Box::new(io::stdout()) as Box // oh well
            } else {
              panic!("Can't open log file: {}", e);
            }
          },
        }
      } else {
        Box::new(io::stdout()) as Box
      };

      // Commit the tx if we get these signals:
      let signal = chan_signal::notify(&[Signal::INT, Signal::TERM]);

      let negotiator = OpenSsl::new().unwrap();
      let url = env::var("MYAPP_DATABASE").unwrap_or("postgres://myapp_test:secret@localhost:5432/myapp_test".to_owned());
      let tls = if url.contains("@localhost") { TlsMode::None }
                else { TlsMode::Require(&negotiator) };
      let conn = Connection::connect(url, tls).expect("Can't connect to Postgres");
      let db = make_db_connection(&conn); // defines a bunch of prepared statements
      
      // now we can do stuff . . . 

    }
I would really like to have just this:

    let log = open_log();
    let db = prepare_db();
But those don't work, because all the temporary values are going to fall off the stack when the helper functions return. I wish rust were smart enough to make the functions put the values directly in the caller's stack frame. Alternately, I wish rust would let me say that all those temporary values should live as long as the returned thing (log and db), so it can keep them around even if I don't have variables for them.

I thought maybe macros would help here, since there is no new stack frame, but they still introduce a new scope that limits the lifetime of the temporary variables.

Even worse, if I want to write tests for functions that use the log and db, I need to repeat all that code again and again.

I think the answer is to use Box here? I haven't worked that out yet, but it definitely feels harder than it should. And even if I can make it work, I'm a little sad that I have to give up stack-based allocation.

I've also read that the answer might be OwningRef (https://crates.io/crates/owning_ref), but I'm not sure yet. I wish the Rust book had a section about it. It seems like Cow and Rc might also help me---I don't think so, but I'm not positive yet. Covering these allocation-related crates in a systematic way would be nice.

Anyway, I'm just a Rust newbie, but it sounds like the ergonomics effort is (partly) for newbies like me, so I'm trying to express my struggles in terms of a pattern that the Rust team could optimize for. It seems like something that people would hit quite often. I'm sure there is an answer to what I'm trying to do, so my point is that maybe it should be easier to find, or at least better documented.

Re: Rust's language ergonomics initiative

#8

I still can't get my head around rust. While all those features definitely make sense, I find it very confusing sometimes. Is there something like rust for c++ programmers?

Is this before or after leafing through their book? Because I think there are at least two stages of not-understanding Rust. One is before taking a look at the book and docs in which you can't make any sense of it at all. Another one is after scanning the book and trying some of the examples in which you really start to understand how you can't make any sense of it at all.
Post reply on HN