Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

trait类型安全 #21

Closed
freheit889 opened this issue Jul 4, 2020 · 4 comments
Closed

trait类型安全 #21

freheit889 opened this issue Jul 4, 2020 · 4 comments
Labels
learning Learner discussions

Comments

@freheit889
Copy link

freheit889 commented Jul 4, 2020

在 rust编程之道书里 有这么一段程序

trait Foo{
      fn bad<T>(&self,x:T);
      fn new()->Self where Self:Sized;
}

如果说我们想将一个trait作为trait对象使用,那么我们就必须保证trait中的所有方法都是对象安全的

在前面对象安全中 有一点说到对象安全必须不包含任何泛型参数

但是书上上述代码中 这个trait是对象安全的

请问是我的理解有问题吗?

@jwnhy
Copy link
Contributor

jwnhy commented Jul 4, 2020

不是太明白你的意思,我手里没有编程之道的书,希望你能把原文拍照发上来之类的。

代码最好使用 ``` 来进行格式化

@freheit889
Copy link
Author

不是太明白你的意思,我手里没有编程之道的书,希望你能把原文拍照发上来之类的。

代码最好使用 ``` 来进行格式化

好的 我订正下

@jwnhy
Copy link
Contributor

jwnhy commented Jul 4, 2020

根据The Book所说,你贴的这段程序里刚好违背了对象安全的两个要求。

  • The return type isn’t Self.
  • There are no generic type parameters.

我前往 Rust 编程之道 的勘误列表,找到了这个 Issue,应该是作者的笔误。
#137 关于trait的Self类型参数不能被限定为Sized

@freheit889
Copy link
Author

根据The Book所说,你贴的这段程序里刚好违背了对象安全的两个要求。

  • The return type isn’t Self.
  • There are no generic type parameters.

我前往 Rust 编程之道 的勘误列表,找到了这个 Issue,应该是作者的笔误。
#137 关于trait的Self类型参数不能被限定为Sized

好的 谢谢你! 勘误列表已收藏

@jiegec jiegec closed this as completed Jul 4, 2020
@Tuyixiang Tuyixiang added the learning Learner discussions label Jul 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
learning Learner discussions
Projects
None yet
Development

No branches or pull requests

4 participants