Hacker News new | ask | show | jobs
by insanitybit 1262 days ago
Unless you're using `'static` you will still run into lifetimes with Cow, which I suspect a lot of people will find difficult. I'd suggest just using clone, learning the broader language, and learning about lifetime stuff later.

Lifetimes aren't hard or complicated, what's complicated is understand their interaction with other features (closures) when you don't even understand wtf closures in rust are, or traits, etc. I'd just focus on the other stuff for as long as you can.

1 comments

> Lifetimes aren't hard or complicated,

Untrue - read https://users.rust-lang.org/t/what-is-a-good-mental-model-of.... Even Rust aficionados can't describe how they work without vast screeds. Or if you think the posters there are just wrong, try the RFC on which the current implementation is based: https://rust-lang.github.io/rfcs/2094-nll.html. You truly don't find that hard or complicated?

It may be the case that people can use simplified subsets of the language to ease their way. But when a Rust advocate says that in effect to understand lifetimes you have to read the standard library source (as is written in the forum thread linked above), because no current documentation is comprehensive enough to cover it, then you know the whole thing itself is pretty complex.

> You truly don't find that hard or complicated?

This is like saying: "You think a 64bit integer is simple? OK, let's dig into the C memory model, twos complement, overflow CPU flags, how CPU caches are implemented, architectural nuances that can lead to unsynced writes from registers to RAM, etc". In reality most people can just learn that it's a number that holds 2^64 values, not complicated. If you want to really understand integers though go read the intel manual and the C memory model specification.

Are lifetimes complicated? If you're a compiler developer who needs to consider the implications across all features and edge cases, yes. If you're learning the language and you just want to write "find me a &str in another &str but don't clone it", no, it's not complicated at all.

The vast majority of beginners can just learn "& means any number of readers and no writers, &mut means one writer and no readers".

> The vast majority of beginners can just learn "& means any number of readers and no writers, &mut means one writer and no readers".

But 'beginning' is never the hard part of learning any programming language, at least for a programmer. The hard part is going from having learned the basics to getting stuff done. Rust is harder than any other mainstream language to do that in. The writers in that thread can't even describe their mental model (NOT the underlying tech as you claim) of lifetimes without vast elliptical descriptions. And they can't point to any straightforward documentation.

I have learned multiple programming languages, of many different paradigms, and never had the trouble getting to the stage of writing useful software that I have had with Rust. I've witnessed the same again and again with all the people I know - all professional programmers. In fact I'm the only one I know who's stayed with it.

I find the denial of Rust's difficulty (and not even centred on the borrow checker - it's the use of just about every commmon library) just very very strange. Odd enough (and distant enough from the obvious, adn the experience of every person I've known) that I find it completely incomprehensible.

> Rust is harder than any other mainstream language to do that in.

I’d argue C and C++ are both harder. It’s hard to even get these to build once you move past trivia examples (including libraries etc).

> I find the denial of Rust's difficulty (and not even centred on the borrow checker - it's the use of just about every commmon library) just very very strange

Some of us just didn’t find it that hard. Like, I get that its hard for people in theory, because I’ve seen enough people express this that I believe it must be a common experience. But my personal experience was a couple of weeks of slow progress and lots of puzzling, and then everything became much easier, coupled with a joy that most of the language is so much better designed than the previous generation of languages I was used to using (e.g. it actually has sum types, and everything is an expression - why on earth doesn’t every language work like this)

C is a simple language. Many people have quickly become proficient at it with only K&R to work from, and that is a thin book. The C language has many deficiencies but I don’t think many people would characterize it as difficult to learn among its peer programming languages. It has very few concepts that you need to learn.

C++, on the other hand, is exceedingly complex and a difficult language to become proficient in, though modern C++ is an improvement in that regard. Some of this is due to decades of legacy cruft, some is due to it being an atypically expressive systems language, all of which has been stirred together.

C is a simple language if you only want to do simple things with it (like say, numerical calculations). As soon as you want to do anything more sophisticated you find all the complexity in other languages and more comes back just as library APIs rather than language-level features.
> > Rust is harder than any other mainstream language to do that in.

> I’d argue C and C++ are both harder. It’s hard to even get these to build once you move past trivia examples (including libraries etc).

Agree I overstated that. What I mean is that, out of the difficult to learn languages, Rust is the only one whose community for opaque reasons denies the difficulty.

> Some of us just didn’t find it that hard.

Why the difference I don't know. So far everyone I know who has learned it has dropped out because they just couldn't get practical traction with it. My guess is that most people who say they don't find it hard are either longstanding C/C++ programmers, and/or are learning at work where they have immediate help & scaffolding.

What is odd is that rather than responding to difficulties with a curious "Oh, I didn't find it hard, I wonder where the difference lay", Rust advocates tend to come back with an aggressive 'proof' that Rust is factually not hard to learn (with implications we can all guess at). No other programming community frequently does this in my experience (and I have brushed against many). That community attitude itself must have causes that are worth thinking about.

I know C and C++ since the mid-90's, and have delivered several products into production with them, still have issues today when dealing with some cases where the borrow checker should be fine (from human borrow checker point of view), but it doesn't get it.

There are also these patterns one has to learn like applying references to numbers in some corner cases, e.g. func(&123), and the fact polominus is being developed shows there is still room for improvement on the borrow checker logic.

> My guess is that most people who say they don't find it hard are either longstanding C/C++ programmers, and/or are learning at work where they have immediate help & scaffolding.

Neither of those were the case for me. In fact, I'd previously tried and failed to learn C (to a level where I could achieve something useful). I did learn at work, which gave me time as I was able to work on it full time, but I had no help available as nobody else at the company knew Rust at the time.

> Rust advocates tend to come back with an aggressive 'proof' that Rust is factually not hard to learn (with implications we can all guess at). No other programming community frequently does this in my experience (and I have brushed against many). That community attitude itself must have causes that are worth thinking about.

I somewhat agree this behaviour isn't great. I suspect the cause is a whole bunch of people saying that Rust is too hard, and that the industry therefore shouldn't bother with it.

Factors I'd guess at:

- Previous experience with a language that uses functional patterns is a huge plus. Even if that's very lightweight functional patterns like you might find in JavaScript or Kotlin (past exposure to something like Haskell or Scala would be even better). - Previous experience with C++ helps. Especially if you write "modern" C++ with smart pointers, etc as this is rather close to how Rust works. - C experience can be a positive or a negative. If you are the sort of C programmer who is hyper aware of object lifetimes and programs carefully and defensively it'll likely help. If you're the kind of C programmer who likes to play fast and loose with safety so long as it works in practice then it'll likely hinder as you'll expect to be able to do things that you can't. - Java/C# and other strongly OOP languages can make learning Rust harder. OOP code tends to be full of graphs of objects that all point to each other, and you just can't do this in Rust. So if that's how you're used to programming your going have to completely relearn how to structure code.

And I guess some of it's just going to be how comfortable people are with abstraction. Rust is definitely heavier on the mathier side of programming (with a sophisticated type system, etc). Personally I find that makes it easier not harder than languages like Go and C which are much simpler in this respect, as when reading code in those languages I often find it hard to pick out those forest from the trees. But from what I've seen, programmers are pretty split on their preferences on this issue, and I imagine for some people this would make it harder.

The rust borrow checker has been described in trivial terms many times. The first time I had it explained, as I recall, was as a book.

You own a book.

`&` - You can lend others the book, they can't fuck with it. `&mut` - You can lend the book to one person, they can fuck with it `move` - You give someone else the book, it's theirs now

Or `many reader NAND one writer`

Is this a complete explanation? No. But it's quite simple and you can be plenty productive with just this amount of understanding.

Your experience is not my experience. I used Rust for the first project I wrote as an intern after dropping out, having never used it before, and I even used it to interact with mysql, which I had never used before.

At my last company I had multiple people pick up rust in a matter of days.

I'm not denying rust as being difficult, I'm saying it's easy for some and hard for some. I found it easy, I was writing productive code on day 0 with virtually no preparation other than that I wanted to try it out. Many people find it easy. Obviously some people, many people, find it hard. Life's weird like that.

OK, simple issue that a beginning Rust user runs into immediately:

  - function arguments are moved into the called function

  - you can call a function with a ref, and then you can keep using it in the calling function, because there is a blanket impl of Copy for refs

  - you can call a function with a ref mut, and then you can keep using it in the calling function because ... ???
Because when you pass by reference, you lend the value (or to phrase it the other way around: the function borrows the value). And so when the function’s done, it gives you the value back (because that’s how borrowing works, in the everyday sense of the word, as well as in th Rust sense)
You add '.clone()' or '&', the code compiles, you move on.
From the standpoint of being productive in Rust, this is what you need to know:

* An object has a lifetime. This generally lasts until overwritten or destroyed at end of scope.

* You can loan out references to this object. You can either have a single &mut, xor an unlimited number of & references, but not both.

* References cannot outlast the lifetime of the object they point to.

* If you have a &mut reference to an object, you can give out another &mut reference to the same object. But you can't use the first reference for the duration of the second.

* Lifetimes can be named, and you can express criteria of the form "this lifetime must at least/most this long."

* Bonus: if you have &mut x, you can give out &mut x.a and &mut x.b at the same time. But you can't give out &mut x while such a subreference is live.

And... that's really all you need. Yeah, there's a lot more rules, and there's definitely fun edge cases around things like temporary objects' lifetimes. But there's no need to know all of that stuff. If you get things wrong, the compiler will come back and slap you in the face and give you an error message--that's the selling point of Rust, getting lifetimes wrong is an error, not silent nonsense--and your task at that point is to figure out if you're really breaking a cardinal rule (two or more mutable references to the same memory location, or references not lasting long enough), or if you didn't enforce sufficiently strict requirements for the bounds of the lifetimes. And the rust compiler is pretty good at telling you what you have to do to get necessary lifetime bounds (sometimes too good--it can suggest fixes to lifetime bounds even when such bounds are unachievable).

I'd compare this to things like name lookup and overload resolution in C++, which are actually horrendously confusing algorithms that almost nobody understands in their entirety. Yet plenty of people can be productive in C++ because the general principles are well-understood, and if you're at the point where you need to descend into the morass of exceptions and exceptions-to-exceptions, you're probably writing confusing code to begin with.

But I've never heard a C++ programmer deny that it's a large and difficult language.

I personally never really had a problem with ownership model - probably because what I was doing fell into the simple buckets. Rust's difficulty goes well beyond ownership. I just thought the Rust user forum thread I quoted was a gently funny example of the Rust community's denial: "Rust's not hard, but to get a decent mental model of the borrow checker you need to read the standard library, or if not here's 10000 words on how I think of it". I'm not presenting it as "proof" that Rust is hard - the fact that I couldn't do anything practical with it after more troublesome attempts than with any other language is plenty enough for me to know that. Neither do I think difficulty is 'bad'. Denying it can be though.

That's someone who's defining "mental model" as effectively "build a formal model of how this things worth without actually using formalism for everything." Of course everyone in that thread is coming up with very complicated stuff, because OP explicitly asked them to do so.

Yes, Rust's borrower checker is more complicated than equivalent features in other languages. If you read the writings of the Rust language developers, you'll notice that they basically admit that it's where Rust sinks its entire complexity budget, so there's no room to spend it anywhere else. But I don't think it's too complex--it's not complex enough for average developers to not be productive. I would contrast this with C++'s template metaprogramming, which I believe to be too complex for average developers to be productive (e.g., trying to write templates that switch based on the types of the parameters, especially before if constexpr).

> Rust is harder than any other mainstream language to do that in.

It's not hard, you just add boilerplate. Cloning data, using interior mutability, or adding ref counting to deal with cases where multiple "owners" can keep an object around independently.

Then removing the boilerplate is how you do optimization, once you've gotten things to work. The opposite of other languages where low-level code is the most verbose and least intuitive.

> It's not hard,

Yep, that's the standard Rust aficionado flat insistence: "You're finding it hard. You're just wrong". Yet with more learning time than I've put into any other language I've learned, I have been unable to use Rust for real (ie. beyond beginner toys). A quick count of langs I've used professionally comes to about 10; I've learned many more to play with, much more successfully than with Rust, in much less time than I've spent with Rust. This reflects the experience of everyone I know who's tried it out (I'm the last man standing, having another crack at it this year).

The cultural peculiarity of this is that, in a field that generally lauds effort and intelligence, Rust advocates uniquely consider 'hard' or 'difficult' to be negative criticism. I don't hear this from (say) Haskell or Scala programmers. I certainly don't consider difficulty a negative - it depends on whether the payoffs are commensurate, and for Rust I think they are (I'd just quit otherwise).

As I'm 100% satisfied (no matter how often I'm unconvincingly 'corrected') that Rust is indeed harder than most programming languages to learn, I can only categorise this peculiarity as denial. Denial usually has ideological origins. I'm not entirely sure what they are in this case, though I have some ideas. These do feed into a sense that although I like the language, I don't think I like the Rust community much. I get strong religious/culty vibes from it - similar to the worst of the Linux community (for the record I also am a f/t Linux user, and I think that community has moderated considerably over time).

Just to be clear, again, lots of people find rust difficult. I found it easy. Lots of people find it easy. That's interesting.

Saying "rust is difficult" is silly to me because... it wasn't for me. Saying "rust is easy" is less silly to me because for me it was, but obviously for you it will be more silly because it isn't easy for you.

You'll find that many in the rust community are in fact very very sympathetic to your view that it's too hard to learn.

Ok, so how do I do one writer and one or more readers?