If we were willing to leak memory, then we could write […] Box::leak(Box::new(0))
In this example, you could have just made a constant with value 0 and returned a reference to that. It would also have a 'static lifetime and there would be no leaking.
Why does nobody seem to be talking about this?
My guess is that the overlap in use cases between Rust and C# isn’t very large. Many places where Rust is making inroads (kernel and low-level libraries) are places where C# would be automatically disqualified because of the requirements for a runtime and garbage collection.
In this example, you could have just made a constant with value 0 and returned a reference to that. It would also have a 'static lifetime and there would be no leaking.
I believe the intention was to demonstrate something that works with runtime values too, and a constant 0 does not.
Btw you can just write &0 to do what you proposed, there’s no need for an explicit constant/static.
I can agree that the example function is not the best usecase. But the point still stand that there’s no realistic escape hatch from lifetimes and memory management in Rust.
Cow does not work when you are actually required to return a reference, e.g. if you’re working with some other crate that requires that. Cow also has some more strict requirements on reborrows (i.e. you can reborrow a &'short &'long T to a &'long T, but you can only reborrow a &'short Cow<'long, T> to a &'short T).
LazyLock can solve very specific issues like static, but is not a general escape hatch. Again, the example is not the best to showcase this, but imagine if you have to perform this operation for an unknown amount of runtime values. LazyLock will only work for the very first one.
but imagine if you have to perform this operation for an unknown amount of runtime values
This is a poor argument. You dont write code like this in rust. If you can find a situation where it is an actual issue we can discuss things but to just say imagine this is a problem when it very likely is not a problem that can be solved in a better way at all let alone a common one is a very poor argument.
Typically when you want an escape from lifetimes that means you want shared ownership of data which you can do with an Arc. Cow and LazyLock can also help in situations - but to dismiss all these for some imagined problem is a waste of time. Comes up with a concrete example where it would help. Very likely you would find another way to solve the problem in any realistic situation you can come up with that I would suspect leads to a better overall design for a rust program.
I would say this is just a straw man argument - but you have not even created a straw man to begin with, just assume that one exists.
I perfectly agree, that would be horrible code! I would generally try to restructure my code, making it better fit the actual lifetimes of the data I’m working with. The point in the article is that you can’t really escape from this. I’m not arguing this is a real problem, and I don’t think the article is neither, just pointing out that this is something you can easily do in C# and not in Rust. It’s just a difference between the two languages.
What? You can easily escape from it if there are better alternatives you can use. Pointing at one language and saying it is not easy to code like it is another language is a pointless argument. You can do that about any two languages. They all differ for good reasons and as long as you can solve similar problems in both, even if in different ways then what does it matter that you cannot do it in the same way?
What? You can easily escape from it if there are better alternatives you can use.
So there is no general escape hatch.
Pointing at one language and saying it is not easy to code like it is another language is a pointless argument.
I’m not arguing that it is easier to code in C# than in Rust, just that this particular escape hatch is possible in C# and not in Rust. It’s just an observation.
They all differ for good reasons and as long as you can solve similar problems in both, even if in different ways then what does it matter that you cannot do it in the same way?
This is a kind of stupid example, but imagine you have to implement some external trait (e.g. in order to work with some dependency) with the following shape:
trait Foo {
fn foo(&self, i: usize) -> &Bar;
}
Which is not too unreasonable, for example it’s very similar to the stdlib’s Index. In order to implement this trait you must return a reference, you can’t return e.g. a Cow or an Arc. The fact that it takes a parameter means there might not even be one single value it has to return, so you can’t even cache that inside of self with e.g. LazyLock.
Of course I’m not saying I would try to reach for an escape hatch if I had to do something like this. I would first try to see if this is an intrinsic problem in my code, or if maybe I can change the crate I’m working with to be more permissible. That is, I would try to look for proper solutions first, though Cow might not always work for that.
&Bar is a reference to something. That something is either a part of self, or a part of the static context. There is no other context because there is no runtime/GC. So there is no logical not-nonsensical scenario where this would be both a valid and a limiting situation in Rust. And this is why your surface analogy to Index is invalid.
If the return value may depend on something other than self or the static context, and still need to be reference-like, then the trait definition is wrong. It should either return a Cow, or go for the obvious generalization of returning impl AsRef<Bar> values. With that generalization, references, Cows, and more can be returned.
There is also the possibility that the trait definition is right, and you (the implementer) are trying to break a (probably) deliberate constraint (e.g. the return value in Index being tied to &self).
I would wager a guess that what you call an escape hatchet is considered a very bad C# style anyway (or will/should be). Just like how mutable statics are considered very bad in Rust 😉
That kind of “escape hatch” also makes reasoning about code a lot harder merely because you have to consider that someone used it somewhere. You literally don’t want “escape hatches” from safety guarantees all over your language.
Cool, that was an informative read!
In this example, you could have just made a constant with value
0
and returned a reference to that. It would also have a'static
lifetime and there would be no leaking.My guess is that the overlap in use cases between Rust and C# isn’t very large. Many places where Rust is making inroads (kernel and low-level libraries) are places where C# would be automatically disqualified because of the requirements for a runtime and garbage collection.
(Note that I’m not the article author)
I believe the intention was to demonstrate something that works with runtime values too, and a constant 0 does not.
Btw you can just write
&0
to do what you proposed, there’s no need for an explicit constant/static.Option
not an option?Cow
s?LazyLock
static?I can agree that the example function is not the best usecase. But the point still stand that there’s no realistic escape hatch from lifetimes and memory management in Rust.
Cow
does not work when you are actually required to return a reference, e.g. if you’re working with some other crate that requires that.Cow
also has some more strict requirements on reborrows (i.e. you can reborrow a&'short &'long T
to a&'long T
, but you can only reborrow a&'short Cow<'long, T>
to a&'short T
).LazyLock
can solve very specific issues likestatic
, but is not a general escape hatch. Again, the example is not the best to showcase this, but imagine if you have to perform this operation for an unknown amount of runtime values.LazyLock
will only work for the very first one.This is a poor argument. You dont write code like this in rust. If you can find a situation where it is an actual issue we can discuss things but to just say imagine this is a problem when it very likely is not a problem that can be solved in a better way at all let alone a common one is a very poor argument.
Typically when you want an escape from lifetimes that means you want shared ownership of data which you can do with an Arc. Cow and LazyLock can also help in situations - but to dismiss all these for some imagined problem is a waste of time. Comes up with a concrete example where it would help. Very likely you would find another way to solve the problem in any realistic situation you can come up with that I would suspect leads to a better overall design for a rust program.
I would say this is just a straw man argument - but you have not even created a straw man to begin with, just assume that one exists.
I perfectly agree, that would be horrible code! I would generally try to restructure my code, making it better fit the actual lifetimes of the data I’m working with. The point in the article is that you can’t really escape from this. I’m not arguing this is a real problem, and I don’t think the article is neither, just pointing out that this is something you can easily do in C# and not in Rust. It’s just a difference between the two languages.
What? You can easily escape from it if there are better alternatives you can use. Pointing at one language and saying it is not easy to code like it is another language is a pointless argument. You can do that about any two languages. They all differ for good reasons and as long as you can solve similar problems in both, even if in different ways then what does it matter that you cannot do it in the same way?
So there is no general escape hatch.
I’m not arguing that it is easier to code in C# than in Rust, just that this particular escape hatch is possible in C# and not in Rust. It’s just an observation.
It does not really matter, but does it have to?
What does that even mean? Can you provide a practical example?
(I’m assuming you’re familiar with
Deref
and autoref/autoderef behaviors in Rust.)This is a kind of stupid example, but imagine you have to implement some external trait (e.g. in order to work with some dependency) with the following shape:
Which is not too unreasonable, for example it’s very similar to the stdlib’s
Index
. In order to implement this trait you must return a reference, you can’t return e.g. aCow
or anArc
. The fact that it takes a parameter means there might not even be one single value it has to return, so you can’t even cache that inside ofself
with e.g.LazyLock
.Of course I’m not saying I would try to reach for an escape hatch if I had to do something like this. I would first try to see if this is an intrinsic problem in my code, or if maybe I can change the crate I’m working with to be more permissible. That is, I would try to look for proper solutions first, though
Cow
might not always work for that.&Bar
is a reference to something. That something is either a part ofself
, or a part of the static context. There is no other context because there is no runtime/GC. So there is no logical not-nonsensical scenario where this would be both a valid and a limiting situation in Rust. And this is why your surface analogy toIndex
is invalid.If the return value may depend on something other than
self
or the static context, and still need to be reference-like, then the trait definition is wrong. It should either return aCow
, or go for the obvious generalization of returningimpl AsRef<Bar>
values. With that generalization, references,Cow
s, and more can be returned.There is also the possibility that the trait definition is right, and you (the implementer) are trying to break a (probably) deliberate constraint (e.g. the return value in
Index
being tied to&self
).I would wager a guess that what you call an escape hatchet is considered a very bad C# style anyway (or will/should be). Just like how mutable statics are considered very bad in Rust 😉
That kind of “escape hatch” also makes reasoning about code a lot harder merely because you have to consider that someone used it somewhere. You literally don’t want “escape hatches” from safety guarantees all over your language.