gpt4 book ai didi

rust - `pub(self)` 可见性与没有 `pub` 属性有何不同?

转载 作者:行者123 更新时间:2023-11-29 07:43:17 28 4
gpt4 key购买 nike

see函数的可见性可以在模块内声明为 pub(self)。这与没有 pub 属性的私有(private)函数有何不同?如果它们没有区别,为什么会存在这种语法?

最佳答案

pub(restricted) 语法在 RFC 1422 中引入.它引入了两种指定可见性的新方法:

  • pub(crate):使项目对整个当前 crate 可见,但不超出。
  • pub(in path::to::module):使项目对路径指定的模块树可见。

pub(self)pub(in self)的语法糖,属于第二类:self只是一个路径像 super, ::foo, bar::baz 等等。这意味着,是的,pub(self) 使项目对当前模块树可见。

因此,pub(self) 等同于省略 pub(self) 声明。这甚至说明了 in the RFC :

As noted above, the definition means that pub(self) item is the same as if one had written just item.

RFC 还提到了支持它的原因:

The main reason to support this level of generality (which is otherwise just "redundant syntax") is macros: one can write a macro that expands to pub($arg) item, and a macro client can pass in self as the $arg to get the effect of a non-pub definition.

关于rust - `pub(self)` 可见性与没有 `pub` 属性有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50905715/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com