-
Notifications
You must be signed in to change notification settings - Fork 26
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
add a simple #2
add a simple #2
Conversation
src/chapter_1/use-types-2.md
Outdated
@@ -308,8 +308,60 @@ where | |||
|
|||
类似的问题意味着用作特征对象的特征不能有返回 `Self` 类型的方法,因为预先编译的代码使用特征对象时将无法知道 `Self` 可能有多大。 | |||
|
|||
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。 | |||
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。(因此trait中不能添加泛型方法,尽管有约束,可以满足约束的类型永远会是无数个) | |||
```Rust |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
将 Rust 改成 rust,并且在代码块前后加一空行
src/chapter_1/use-types-2.md
Outdated
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。 | ||
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。(因此trait中不能添加泛型方法,尽管有约束,可以满足约束的类型永远会是无数个) | ||
```Rust | ||
trait foo{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
代码块之间加空行,方法名和括号之间加一个空格,这样代码看起来好看一些
src/chapter_1/use-types-2.md
Outdated
trait foo{ | ||
fn method<T>(&self, t:T); | ||
} | ||
struct S{} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果 struct 没有成员函数,没必要加 {} 了,直接改成 struct S 即可,不过,这里名字是不是改成 struct Bar 更好?
哈哈,一般 foo 的出现,必有 bar
修改结构体名称,使用cargo fmt格式化,改正Rust为rust |
Add a test to make the restrictions of the trait object clearer.