Live data from Hacker News

My Struggles with Rust

compileandrun.com

91–100 of 329 posts

Re: My Struggles with Rust

#91

Isn't nim built for pythonists to do just this sort of thing?

I think Nim may be the most underdocumented project I've ever used. I spent a week or so with it, but quickly grew extremely frustrated as I was constantly scouring old forum posts to learn how to use the basic features of the language. That is not a tolerable situation for me.

Re: My Struggles with Rust

#92
post #39

I was under the impression that the (somewhat) verbose syntax for error handling and memory management via the type system was a necessary side effect of Rusts entire point of existence: a compiler-guaranteed safe systems language. Neither Python nor C force you in any way to pay attention to errors, making simple scripts much easier to write. I guess I'm just surprised people think that Rust should be as simple to u…

I think the complaint is more that Rust has seemingly tried very hard to make error handling "simple". But in the process it has managed to invent a whole series of new idioms and special syntax that is alien to pretty much everyone. There's a thread in /r/rust about this same article where you can look and see people suggesting all sorts of ways to write this that are split into clear sedimentary layers depending on…

As someone who's been doing C++ for a close to few decades I's challenge you on that. In C++ you need to understand:

Exceptions(and the runtime/memory costs they incur by pulling in RTTI)

ERRNO(on relevant *nix platforms)

Lifetimes tied to objects when things fail(this is a big one)

Plus any library-specific hackery(I've seen raw strings as errors before)

In contrast I've been writing Rust for ~1.5 years now and each library I've used is consistent and follows common patterns. Much like a lot of people don't grok functional until they understand the common patterns, so it is with Rust too.

Re: My Struggles with Rust

#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. Sure some overly complex early syntax ideas like different ones for different pointer types vanished in newer Rust releases. Now it's probably too late to improve the syntax.

Re: My Struggles with Rust

#94
post #4

My main gripe with Rust so far has been the unnecessary profusion of Result types, making it hard to process and forward errors. Case in point: the example in the article from the rust documentation that converts errors to strings just to forward them: https://doc.rust-lang.org/book/error-handling.html#the-limit... In practice, I find a type like Google's util::StatusOr ( https://github.com/google/lmctfy/blob/master/…

> Case in point: the example in the article from the rust documentation that converts errors to strings just to forward them

This section:

- Shows you how to define your own Result types. They have chosen a String as an example of what you could use as an error type. In practice nobody uses "String" as an error type.

- Concludes by defining a custom error type to use instead of a String. I guess you didn't read that far? In practice nobody "converts errors to strings just to forward them". String was just an example they were using as they built up to defining a custom error type.

Rust errors can be forwarded as simply as "?". The conversions can be handled automatically with "From" traits. The "error-chain" crate takes care of these conversions for you, wrapping the original errors so they're still available (including stack traces), but aggregating them under a set of error types specific to your crate:

https://github.com/brson/error-chain

Re: My Struggles with Rust

#95
post #87

Earlier quoted context omitted.

Agreed, I've never liked 'expect' either; 'or_else_panic(msg)' would be much clearer Edit: 'or_panic(msg)' would be shorter and also good.

I think that was one of the proposed variations, but we ended up picking the shorter .expect to cut down on repetition. We expected (ha) that this function would be used in these one-offs, so we wanted something more efficient.

As an old Java hack, my allowance for repetition is high. Especially when using and IDE that basically writes the code for me ;)

Re: My Struggles with Rust

#96
post #89

I was under the impression that the (somewhat) verbose syntax for error handling and memory management via the type system was a necessary side effect of Rusts entire point of existence: a compiler-guaranteed safe systems language. Neither Python nor C force you in any way to pay attention to errors, making simple scripts much easier to write. I guess I'm just surprised people think that Rust should be as simple to u…

a compiler-guaranteed safe systems language When I need that, I use OCaml.

But OCaml has a non C-like syntax. So it would be even more strange to OP.

Re: My Struggles with Rust

#97
post #31

Hello Justin Turpin! Sorry to hear your struggles with rust. It's always going to be a bit more verbose using rust than Python due to type information, but I think there are some things we could do to simplify your code. Would you be comfortable posting the 20 line code for us to review? I didn't see a link in your post. Anyway, so some things that could make your script easier: * for simple scripts I tend to use the…

While it's too late to change the name "expect", could one create an alias for it and call it say, "on_error"?

I'd expect 'on_error' to take a function as a callback, not a string. But yes, a better named function could be added

Re: My Struggles with Rust

#98

> import json > with open("config.json") as f: > contents = f.read() > config = json.loads(contents) translates to: extern crate serde_json as json; fn read_json() -> Result > { let file = std::fs::File::open("config.json")?; let config = json::from_reader(&file)?; Ok(config) } And > import configparser > config = ConfigParser() > config.read("config.conf") can be translated to: extern crate config; use config::{Conf…

I think this highlights that while there are easy solutions to the problem the OP faced, they're difficult for a newcomer to discover. (I have been using Rust for a couple of months and I'd also have reached for serde and maybe serde_derive to solve the problem).

Hopefully this is something the Libz Blitz[0] will solve with their Rust Cookbook[1]. (You could almost but not quite arrive at as simple a solution from chapters 1 and 2).

[0] https://blog.rust-lang.org/2017/05/05/libz-blitz.html

[1] https://brson.github.io/rust-cookbook/intro.html

Re: My Struggles with Rust

#99
post #89

I was under the impression that the (somewhat) verbose syntax for error handling and memory management via the type system was a necessary side effect of Rusts entire point of existence: a compiler-guaranteed safe systems language. Neither Python nor C force you in any way to pay attention to errors, making simple scripts much easier to write. I guess I'm just surprised people think that Rust should be as simple to u…

a compiler-guaranteed safe systems language When I need that, I use OCaml.

And when you need good support for multicore, you use...?

Re: My Struggles with Rust

#100

Earlier quoted context omitted.

The java optional api uses orElseThrow which I think is quite clear.

It is, but also quite long, which I do not think suits Rust naming conventions.

Not really though: 'unwrap_or_else' is of similar length.
Post reply on HN