Weak is essentially the same as an Rc modulo not keeping the value alive on its own. In particular, you can upgrade() it to an Rc without any extra allocation or copying.
To prevent Option<Weak>. Weak already have two state (is possible convert to Rc and it is impossible).
So in some function I want return Weak::default()/Weak::new() if no Rc available,
but to prevent redundant memory allocation, I have to return Option<Weak>,
and introduce instead of 2 variants, 4 variants only 2 of them have sense.
What do you mean by "no Rc available"? Are you not starting out with an Rc to begin with? I've not really seen any standalone Weak usage - it's usually obtained from downgrading an existing Rc.
What are the semantics/intention of returning a Weak::default() there? An Option<Weak<...>> makes more sense to me anyway - why manufacture a "fake" weak if there's no Rc it's attached to?
Ordinary semantic, I thought - "I give you weak reference, so check it every time you use it and forget about it, when no Rc behind it`.
So user of this function should handle two variants. In every moment Weak may become invalid
because of Foo::a resize, so I can not see any difference between index out of scope and ordinary return.
Why I should force user to handle 3 variants: Some(Weak with no RC), Some(Weak with RC), None,
instead of 2 Weak with no RC and Weak with RC?
As I understand it, both Rc<T> and Weak<T> need to point to some memory block which counts how many weak and how many strong pointers there are. This memory block also stores the value.
It looks like there is no way around the allocation based on the implementation of Rc and Weak.
I was wondering, how anything can be deallocated when all strong references are dropped. Because the memory location where the Weak<T> pointers point needs to stay valid. If there are weak pointers remaining, then only the inner value is dropped. A Rc<u64> stores all information in .value and nothing happens to it. But a Rc<String> can drop its content. The capacity, length and (invalid) pointer will stay in memory until the last weak reference is dropped.
The allocated memory is uninitialized. Pretty much of no harm. If you want to speed up code (one way;) cloning an existing Weak will avoid extra allocation.
Once the last Rc is dropped the value is dropped but not deallocated.
When last Weak is dropped the allocation with uninitialised value is dealloc.
In addition to the things other people have mentioned, by always allocating (instead of, say, allowing a null pointer internally), it's never null, and thus gets layout-optimized so the sizes of Option<Rc<T>> and Rc<T> are the same -- letting people choose between allocate-for-empty and null-check-each-access themselves without overhead.