3 ms·
This effect of <'a> spreading everywhere is very frequently a novice mistake of putting temporary scope-bound loans (AKA references) in structs that aren't temp
by pornel 3y ago
This effect of <'a> spreading everywhere is very frequently a novice mistake of putting temporary scope-bound loans (AKA references) in structs that aren't temporary views themselves.
People used to C or C++ perspective tend to reflexively (over)use references to avoid copying, but references in Rust are to avoid owning, and not-copying is handled in different ways.
BTW, apart from edgiest of edge cases, &String is a useless type in Rust, because String already stores data "by reference" and is never implicitly copied. For loans it's generally better to deref to &str.
- pdimitar 3y ago> and not-copying is handled in different ways Can you give a few examples?
- imetatroll 3y agoI am a novice when it comes to rust but as I recall the instant you put anything that isn't a scalar type you need lifetimes. Is that what the person you are replying to frustrated with? How would not copying be handled? Sorry for the basic questions. Your comment tickles something in my brain that suggests I need to know.
- telios 3y agoVery rarely do you need to pass around a lifetime-bound struct or enum that isn't _also_ meant to be temporary. (One database pool has database connections lifetime bound, for example, but the pool still owns the connection.) When I do, I generally eat the cost and put it behind a Rc/Arc, where cloning is cheap. It is 'borrowing' because you don't own it, not because you don't want to clone/copy it. Sometimes it is cheaper to borrow than it is to clone/copy; sometimes it is not.
- frfl 3y agoI feel like this could be a great in depth blog post...unless such a blog post already exists? It would greatly benefit novices and those who haven't gotten to that stage where this mentality kicks in.
- tracker1 3y agoThere's a pretty good YouTube video I saw pulling all the types of strings in rust. I find it confusing still... Been wanting to do a few things that would mean dealing with it streams that could be cp437 or utt8, generally and haven't been quite sure how I want to deal with it.
- iknowstuff 3y agoEither Vec<u8> or https://github.com/BurntSushi/bstr https://github.com/BurntSushi/bstr
- tracker1 3y agoI'm thinking I want to convert to/from utf8 at the edges even for the encoding... Namely web/terminal, then the internal interfaces and relay in utf8, then back to co437 for door game interaction. Basically a BBS door running service, with some extra niceties internally.