# Mistake in ptr::from\_ref() doc comment on temporary lifetime?

**URL:** <https://internals.rust-lang.org/t/mistake-in-ptr-from-ref-doc-comment-on-temporary-lifetime/21800>\
**Category:** documentation\
**Created:** [November 1, 2024, 3:56pm UTC](https://internals.rust-lang.org/t/mistake-in-ptr-from-ref-doc-comment-on-temporary-lifetime/21800 "2024-11-01T15:56:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cdbennett](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cdbennett/32/12055_2.png) [@cdbennett](https://internals.rust-lang.org/u/cdbennett)\
**Post date:** [November 1, 2024, 3:56pm UTC](https://internals.rust-lang.org/t/mistake-in-ptr-from-ref-doc-comment-on-temporary-lifetime/21800/1 "2024-11-01T15:56:04Z")

</div>

I think there is a mistake in the explanation related to temporary lifetime extension in the documentation for [std::ptr::from\_ref()](https://doc.rust-lang.org/stable/std/ptr/fn.from_ref.html):

> Note that this has subtle interactions with the rules for lifetime extension of temporaries in tail expressions. This code is valid, albeit in a non-obvious way:
> 
> ```rust
> // The temporary holding the return value of `foo` has its lifetime extended,
> // because the surrounding expression involves no function call.
> let p = &foo() as *const T;
> unsafe { p.read() };
> 
> ```
> 
> Naively replacing the cast with `from_ref` is not valid:
> 
> ```rust
> // The temporary holding the return value of `foo` does *not* have its lifetime extended,
> // because the surrounding expression involves no function call.
> let p = ptr::from_ref(&foo());
> unsafe { p.read() }; // UB! Reading from a dangling pointer ⚠️
> 
> ```

In the first snippet, the comment says the lifetime is extended because the expression "involves no function call", which seems correct.

But the second snippet says the return value "does _not_ have its lifetime extended" ... "expression **involves no function call**". I think this should say " **does involve a function call**".

Am I correct? Didn't want to submit a PR without getting someone else to confirm my understanding first.

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [November 1, 2024, 5:22pm UTC](https://internals.rust-lang.org/t/mistake-in-ptr-from-ref-doc-comment-on-temporary-lifetime/21800/2 "2024-11-01T17:22:48Z")

</div>

I believe you're correct. This looks like an unfortunate copy/paste error in the docs, and the two definitely shouldn't both say the same thing.

---

<div class="post-metadata">

**Author:** ![steffahn](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/steffahn/32/13288_2.png) [@steffahn](https://internals.rust-lang.org/u/steffahn)\
**Post date:** [April 25, 2026, 5:23pm UTC](https://internals.rust-lang.org/t/mistake-in-ptr-from-ref-doc-comment-on-temporary-lifetime/21800/3 "2026-04-25T17:23:40Z")

</div>

Closed on April 25, 2026, 5:23pm UTC

This topic was automatically closed 540 days after the last reply. New replies are no longer allowed.
