Why does a MutexGuard require a lifetime parameter in structs but not in function return types? [duplicate]

烂漫一生 提交于 2019-12-06 05:42:43

This is more or less arbitrary design decision in Rust.

In functions there's lifetime elision, where the compiler guesses what lifetime the struct could have based on lifetimes of references in function arguments.

When you have foo(&'a self) -> Struct<'a> there's only one lifetime possible (apart from 'static). This is such a common case that Rust allows this to be implied for convenience: foo(&self) -> Struct.

Definition of references in structs wasn't deemed to be common and unambiguous enough to also have elided lifetimes, and the desire to have explicit lifetime definitions won.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!