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

add a simple #2

Merged
merged 3 commits into from
Apr 21, 2024
Merged

add a simple #2

merged 3 commits into from
Apr 21, 2024

Conversation

Aria-iu
Copy link
Contributor

@Aria-iu Aria-iu commented Apr 20, 2024

Add a test to make the restrictions of the trait object clearer.

src/chapter_1/use-types-2.md Show resolved Hide resolved
@@ -308,8 +308,60 @@ where

类似的问题意味着用作特征对象的特征不能有返回 `Self` 类型的方法,因为预先编译的代码使用特征对象时将无法知道 `Self` 可能有多大。

具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。(因此trait中不能添加泛型方法,尽管有约束,可以满足约束的类型永远会是无数个)
```Rust
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

将 Rust 改成 rust,并且在代码块前后加一空行

具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。
具有泛型方法 `fn method<T>(t:T)` 的特征允许存在无限数量的实现方法,适用于所有可能存在的不同类型 `T`。这对于用作特征约束的特征来说是可行的,因为无限集合的可能的泛型方法在编译时变为有限的实际调用的泛型方法集合。对于特征对象来说,情况并非如此:编译时可用的代码必须应对运行时可能出现的所有可能的 `Ts`。(因此trait中不能添加泛型方法,尽管有约束,可以满足约束的类型永远会是无数个)
```Rust
trait foo{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

代码块之间加空行,方法名和括号之间加一个空格,这样代码看起来好看一些

trait foo{
fn method<T>(&self, t:T);
}
struct S{}
Copy link
Contributor

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

@Aria-iu
Copy link
Contributor Author

Aria-iu commented Apr 21, 2024

修改结构体名称,使用cargo fmt格式化,改正Rust为rust

@lispking lispking merged commit 62fce6c into rustx-labs:main Apr 21, 2024
@lispking lispking mentioned this pull request Apr 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants