| > This is a good thing. This is dogmatic. In can be a good thing sometimes, in some domains. In other instances it can just be a pain. Let's say I'm using a 'names' library that provides the following utility function: first_name<'a>(name: &'a str) -> &'a str
My code happily uses this function (which we can assume is not performance critical). At some point, the author of the library notices that there are cultures where the nearest analogue of a 'first name' is not always a contiguous substring of the full name. To fix this bug they must change the function's type. They may choose e.g.: first_name<'a>(name: &'a str) -> Cow<'a, str>
I'd like to update my version of the library to get the bug fix, but can't do this for free. I have to update the calling code, and possibly even change some of my own internal data representations. This contrasts with pretty much any GCed language. For example, in Go the type of the function would just be func firstName(name string) string
and the bug fix would require no change in the API.Now let's relate this example back to what you originally said in response to kaba0: >> [kaba0:] A libraries next version which switches up some internal representations memory handling should ideally not mess up your application > It doesn't have to because internal memory representation can and should be abstracted out, and Rust gives a plethora of tools to do that. The above is a simple example of why this is not true. Any time you write a function that returns a reference, you are limiting the changes you can make to internal representations (both in the library itself and the calling code) without making a breaking API change. Please don't respond by saying "this API was badly designed in the first place!" Most languages don't give you the opportunity to design APIs badly in this particular way. If all APIs were perfectly designed on day one then of course we'd never have to worry about API changes. Again, none of this is to bash Rust. I just think it is important to be realistic about the downsides as well as the upsides of Rust's ownership system. |
Similarly by writing this in Go:
you promised the function accepts a string.Then you want to change the semantics (the contract) of the function, break your promise and you complain you have to change the signature. You can't do that in any statically typed language.
In Go's case, if you suddently wanted to change the memory representation of name to something else than string, e.g. to a name struct:
then obviously you have to change the signature. This is the same problem.If you don't want your caller to be affected by lifetimes, just don't specify them in the signature:
this is perfectly fine in Rust.You may say it might be slow because it forces a copy, and forces a particular string implementation. So as I said, Rust gives you tools to abstract out the implementation details:
The flexibility goes actually much further than just relaxing the lifetimes. With this signature I actually can change the name representation from String to any type that can be exposed as a slice, with no additional runtime penalty like virtual calls, which you'd need otherwise in Go/Java.> I just think it is important to be realistic about the downsides as well as the upsides of Rust's ownership system.
So the downside is that it offers you more choices and allows to express more constraints in the signatures, and you can choose wrong. I guess that's quite ok in a general purpose language that wants to be applicable to many different niches.