Live data from Hacker News

My Struggles with Rust

compileandrun.com

191–200 of 329 posts

Re: My Struggles with Rust

#191
post #176

Earlier quoted context omitted.

In case someone cares to understand what this means: - "An Error trait" means that when you define a new type that will store error information, you have to define how it implements the Error interface. - "appropriate From impls"... you are trying to wrap a number of error types in your own special error type, you need to tell the compiler how to convert another specific type into your type new type. There is an inte…

Sorry, I'm trying very hard to learn Rust, and I'm willing to accept a lot of its restrictions, but this didn't clear anything up. Why does every library implement its own Error type? It feels like reinventing the wheel, and it takes a lot of boilerplate code. If you need to distinguish different kinds of errors, why not, for example, have a lot of useful pre-defined error types like Python does?

You can definitely reuse other library's error types. I often do that while prototyping.

The benefit of lib-specific error types, though, is that they can be far more specific.

Re: My Struggles with Rust

#192
Rust has stressed ergonomics of late, yet I sometimes struggle to read Rust code. Obviously, there is value in elegant code, but my question is, would anybody find value in an extremely simple language that could compete with the likes of c/c++? Does something like this exist?

Re: My Struggles with Rust

#193
post #42

Earlier quoted context omitted.

I agree with you but until there is an empirical basis for our opinion-probably-honed-by-years-of-coding, these 2 languages will just continue to chug along without real exceptions My argument would be this: What is a runtime exception, really ? It's a state that the programmer did not handle (either due to lack of thoroughness or flaws in mental model). Suppose the error is just ignored: To this I ask, why would you…

I have run into something very similar in somebody else's python program where they declared a string that was later used (based on some conditionals) to locate a file. The thing is that most of the conditionals were never hit under ideal conditions (like passing all args etc.). It took me a lot of time to track down the bug (also because of the lack of awesome debuggers for Python).

> The thing is that most of the conditionals were never hit under ideal conditions (like passing all args etc.).

Your debugging work in this case might have also been alleviated by a good unit test covering this functionality.

Re: My Struggles with Rust

#194

Earlier quoted context omitted.

I agree with you but until there is an empirical basis for our opinion-probably-honed-by-years-of-coding, these 2 languages will just continue to chug along without real exceptions My argument would be this: What is a runtime exception, really ? It's a state that the programmer did not handle (either due to lack of thoroughness or flaws in mental model). Suppose the error is just ignored: To this I ask, why would you…

I can't speak for Go, but what you describe isn't really a thing in Rust. Functions in Rust that might encounter an "exception" typically return something like `Result ` where `T` is the type of what we hope we get and `E` is a type that encodes the details of the errors/exceptions we might see. It's an enum type that comes in two flavors: `Ok(T)` and `Err(E)`. You can't just go happily along treating an `E` like it'…

Good point. I am not very familiar with Rust (good to hear it seems to treat this well, however!) but a cursory examination of Go code shows that runtime errors can end up being entirely ignored and that seems crazy to me.

> I tend to be more interested in how the features of a language help programmers to keep writing correct and maintainable code as the complexity of a project grows.

So let me guess, you too have worked on very large spaghetti codebases? ;) Because I am also interested in that very same end-goal! And that is actually why I've decided to only focus on functional langs for now (right now it's Elixir but I'm going to be evaluating Haskell, not a huge fan of the JVM langs tho), because the resulting code just feels more maintainable

Re: My Struggles with Rust

#195

The `error-chain` crate [1] exists to get rid of precisely the error handling boilerplate the author has encountered. That's not ideal, though, as I believe that a place for such functionality is in the core language, not a separate library, but it gets the job done. As for the `let mut file` bit, that makes sense to me: a file in the standard library is an abstraction over a file descriptor in the operating system,…

I'd like to see error-chain standardized into the standard library as well. It seems by far the most sensible approach to building Error types.

I'd prefer it if that didn't happen, or at least not for a few years. error-chain involves some pretty hairy macros, which would feel out of place in the standard library. I'd like to wait and see if the community can come up with something more transparent before canonising error-chain.

Re: My Struggles with Rust

#196

Rust has stressed ergonomics of late, yet I sometimes struggle to read Rust code. Obviously, there is value in elegant code, but my question is, would anybody find value in an extremely simple language that could compete with the likes of c/c++? Does something like this exist?

Nim. It lets you get stuff done without having to drink the provably correct kool-aid.

Re: My Struggles with Rust

#197
post #188
post #177

Earlier quoted context omitted.

It might be worth pointing out that this Rust: #[derive(Debug)] enum ConfigError { Io(io::Error), Parse(ParseIntError), } impl From for ConfigError { fn from(err: io::Error) -> ConfigError { ConfigError::Io(err) } } impl From for ConfigError { fn from(err: ParseIntError) -> ConfigError { ConfigError::Parse(err) } } fn read_config() -> Result { Result::Ok(parse_int(read_config_file()?)?) } // given the following fn pa…

I would love a `#[derive(From)]` for newtype structs and enum variants. Would bring Rust error handling back below Java in boilerplate levels. :)

There are some crates that add a derive for errors, but most people tend to use error-chain or quick-error. Both give you the boilerplate pretty much for free, with various other advantages. E.g., using error-chain, you can just write the above code as

    error_chain! {
        foreign_links {
            Io(io::Error);
            Parse(ParseIntError);
        }
    }
and it'll even generate an aliased `Result` type as well as fancy chaining support.

Re: My Struggles with Rust

#198
post #188
post #177

Earlier quoted context omitted.

It might be worth pointing out that this Rust: #[derive(Debug)] enum ConfigError { Io(io::Error), Parse(ParseIntError), } impl From for ConfigError { fn from(err: io::Error) -> ConfigError { ConfigError::Io(err) } } impl From for ConfigError { fn from(err: ParseIntError) -> ConfigError { ConfigError::Parse(err) } } fn read_config() -> Result { Result::Ok(parse_int(read_config_file()?)?) } // given the following fn pa…

I would love a `#[derive(From)]` for newtype structs and enum variants. Would bring Rust error handling back below Java in boilerplate levels. :)

Check out https://crates.io/crates/newtype_derive

Re: My Struggles with Rust

#199
post #93

Rust is important, it has a great chance being a first modern native system language (memory safety). Though, I wish it has less exotic syntax. It's like C++ and Erlang had a baby. Look at modern languages with nice syntax like Go, Julia, Swift and compare it to Rust. Someone coming from C, C++, C#, Java, PHP and JavaScript has to learn a lot of new syntax twists that look uncommon and different for little reason. Su…

It continues to puzzle me when people think of Rust's syntax as particularly exotic. It's much closer to C/Java/JavaScript than e.g. Python or Bash are. Rust still has curly braces for blocks, uses ampersand and asterisk in ways that aren't too far from C, uses dot in a way that's not too far from C or Java and uses less-than and greater-than to denote generics like C++ and Java. Personally, what keeps tripping me up…

> Edit: Plus it makes sense to have types after the variable name when they are optional in most cases (and then to have them in the same order in function signatures for consistency).

Since you brought this up, the one thing that really (but irrationally!) winds me up about Rust's syntax is that variables are:

  let foo: Bar = ... ;
And functions are:

  fn foo() -> Bar { ... }
Why not use a colon for the return type of a function as well?

Re: My Struggles with Rust

#200
post #199

Earlier quoted context omitted.

It continues to puzzle me when people think of Rust's syntax as particularly exotic. It's much closer to C/Java/JavaScript than e.g. Python or Bash are. Rust still has curly braces for blocks, uses ampersand and asterisk in ways that aren't too far from C, uses dot in a way that's not too far from C or Java and uses less-than and greater-than to denote generics like C++ and Java. Personally, what keeps tripping me up…

> Edit: Plus it makes sense to have types after the variable name when they are optional in most cases (and then to have them in the same order in function signatures for consistency). Since you brought this up, the one thing that really (but irrationally!) winds me up about Rust's syntax is that variables are: let foo: Bar = ... ; And functions are: fn foo() -> Bar { ... } Why not use a colon for the return type of…

1. Rust mimics math notation.

2. If one needs variable to hold a function, he declares it like this:

    let foo: fn(i32) -> i32 = ...
The second colon would be confusing.
Post reply on HN