No method named `subtractPointFromPoint` found for type `()` in the current scope

时间:2018-02-03 11:20:45

标签: rust

I have started creating a gaming graphics engine using this article. I chose Rust because I have heard about it and it sounds perfect for creating games. The only problem is I have no experience whatsoever. I have used Python, JavaScript, Java and HTML before.

This is the code I have written so far:

point.rs

// Point class

// Operators
struct point {
    x: i32,
    y: i32,
    z: i32,
}

// Variables
pub fn new(x: i32, y: i32, z: i32) {
    let mut point = (x, y, z);
    //return point;

    impl point {
        fn addVectorToPoint(self: &point, (x, y, z): (i32, i32, i32)) {
            let mut x = x + &self.x;
            let mut y = y + &self.y;
            let mut z = z + &self.z;
            let mut point = (x, y, z);
        }

        fn subtractVectorFromPoint(self: &point, (x, y, z): (i32, i32, i32)) {
            let mut x = &self.x - x;
            let mut y = &self.y - y;
            let mut z = &self.z - z;
            let mut point = (x, y, z);
        }

        fn subtractPointFromPoint(self: &point, (x, y, z): (i32, i32, i32)) {
            let mut x = &self.x - x;
            let mut y = &self.y - y;
            let mut z = &self.z - z;
            let mut vector = (x, y, z);
        }
    }
}

main.rs

mod point;

fn main() {
    let point1 = point::new(1, 2, 3);
    let point2 = point::new(3, 2, 1);
    let newPoint = point1.subtractPointFromPoint(point2);
    println!("{:?}", newPoint);
}

When I run it, I get this:

error[E0599]: no method named `subtractPointFromPoint` found for type `()` in the current scope
 --> src/main.rs:6:27
  |
6 |     let newPoint = point1.subtractPointFromPoint(point2);
  |                           ^^^^^^^^^^^^^^^^^^^^^^

What I am doing wrong?

1 个答案:

答案 0 :(得分:8)

不要指望你可以随意地在你的编辑器中输入代码,它会起作用。编程语言具有必须遵循的语法和结构规则。此外,Rust社区已经付出了很多努力来创建优秀的指南。

例如,如果您阅读The Rust Programming Language,特别是defining and instantiating structsmethod syntax上的章节,您将了解到返回值的函数必须在函数签名中声明返回类型:

pub fn new(x: i32, y: i32, z: i32) -> point { /* ... */ }

您还必须

  • 实际上创建你的结构(let mut point = point { x, y, z }
  • 实际上从函数
  • 返回结构

这可以简化为:

pub fn new(x: i32, y: i32, z: i32) -> point {
    point { x, y, z }
}

如果你没有声明一个返回类型,那就像说一个函数返回空元组一样,也称为单元类型

pub fn new(x: i32, y: i32, z: i32) -> () { /* ... */ }
// Idiomatically written as
pub fn new(x: i32, y: i32, z: i32) { /* ... */ }

错误消息的found for type `()`部分来自哪里。单位类型也是Rust语句的值 - 以;结尾的表达式。这就是你的函数体编译的原因 - 它以;结束。

存在各种其他问题:

  • PascalCase等类型使用Point
  • 使用snake_case表示变量和方法。
  • 将构造函数移动为Point关联函数
  • 构造函数可以返回Point而不是重复名称Self
  • 使用&self代替self: &Point
  • 你的方法没有任何东西。他们取一个值,执行一些操作,然后扔掉结果。您可能想要&mut self
  • 您的方法不接受Point,而是接受数字元组。
  • 您正在尝试更改point1变量,但未将其标记为可变
  • 您需要实施Debug才能打印出Point
#[derive(Debug)]
struct Point {
    x: i32,
    y: i32,
    z: i32,
}

impl Point {
    pub fn new(x: i32, y: i32, z: i32) -> Self {
        Point { x, y, z }
    }

    fn add_vector_to_point(&mut self, (x, y, z): (i32, i32, i32)) {
        self.x += x;
        self.y += y;
        self.z += z;
    }

    fn subtract_vector_from_point(&mut self, (x, y, z): (i32, i32, i32)) {
        self.x -= x;
        self.y -= y;
        self.z -= z;
    }

    fn subtract_point_from_point(&mut self, Point { x, y, z }: Point) {
        self.x -= x;
        self.y -= y;
        self.z -= z;
    }
}

fn main() {
    let mut point1 = Point::new(1, 2, 3);
    let point2 = Point::new(3, 2, 1);
    point1.subtract_point_from_point(point2);
    println!("{:?}", point1);
}

考虑实施AddAssignSubAssign而不是这些自定义方法。

您还可以实施AddSub

impl std::ops::Sub for Point {
    type Output = Point;
    fn sub(self, other: Point) -> Point {
        Point::new(self.x - other.x, self.y - other.y, self.z - other.z)
    }
}

fn main() {
    let point1 = Point::new(1, 2, 3);
    let point2 = Point::new(3, 2, 1);
    let point3 = point1 - point2;
    println!("{:?}", point3);
}
相关问题