我有一个自定义类型 Point
type Point = (f64, f64);
Run Code Online (Sandbox Code Playgroud)
我想将两个Points 加在一起,但出现此错误
error[E0368]: binary assignment operation `+=` cannot be applied to type `(f64, f64)`
--> src/main.rs:119:21
|
119 | force_map[&body.name] += force;
| ---------------------^^^^^^^^^
| |
| cannot use `+=` on type `(f64, f64)`
Run Code Online (Sandbox Code Playgroud)
当我尝试实现 Add 时,出现以下错误:
39 | / impl Add for (f64, f64) {
40 | | #[inline(always)]
41 | | fn add(self, other: (f64, f64)) -> (f64, f64) {
42 | | // Probably it will be optimized to not actually copy self and rhs for each call !
43 | | (self.0 + other.0, self.1 + other.1);
44 | | }
45 | | }
| |_^ impl doesn't use types inside crate
|
= note: the impl does not reference any types defined in this crate
= note: define and implement a trait or new type instead
Run Code Online (Sandbox Code Playgroud)
Add是否可以实施type?我应该用 astruct代替吗?
不,你没有类型Point。不幸的是,该type关键字不会创建新类型,而只是为现有类型创建新名称(别名)。
要创建类型,请使用:
struct Point(f64, f64);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4681 次 |
| 最近记录: |