如何在 Rust 中定义标准类型的实现?
How to define impl on standard types in Rust?
我们如何围绕现有类型定义包装器类型并在其上定义函数?
我尝试了下面的代码,但出现了这个错误
struct Deploy(u8);
impl Deploy {
fn values_yaml(self) -> u8 {
self+1
}
fn chart(self) -> u8 {
self+1
}
}
fn main() {
let a = Deploy(1);
println!("Hello, world! {:?}", a.chart());
}
错误:
error[E0369]: cannot add `{integer}` to `Deploy`
--> src/main.rs:5:11
|
5 | self+1
| ----^- {integer}
| |
| Deploy
|
note: an implementation of `Add<_>` might be missing for `Deploy`
欢迎提出任何建议。
您正在处理元组大小为 1 的元组结构。通过 Rust 中元组的常用语法访问元组的元素:
self.0
或者,您也可以像匹配常规元组一样匹配字段。
let Deploy(resource_type) = self;
我们如何围绕现有类型定义包装器类型并在其上定义函数?
我尝试了下面的代码,但出现了这个错误
struct Deploy(u8);
impl Deploy {
fn values_yaml(self) -> u8 {
self+1
}
fn chart(self) -> u8 {
self+1
}
}
fn main() {
let a = Deploy(1);
println!("Hello, world! {:?}", a.chart());
}
错误:
error[E0369]: cannot add `{integer}` to `Deploy`
--> src/main.rs:5:11
|
5 | self+1
| ----^- {integer}
| |
| Deploy
|
note: an implementation of `Add<_>` might be missing for `Deploy`
欢迎提出任何建议。
您正在处理元组大小为 1 的元组结构。通过 Rust 中元组的常用语法访问元组的元素:
self.0
或者,您也可以像匹配常规元组一样匹配字段。
let Deploy(resource_type) = self;