Rust游戏图形引擎开发报错:未找到`subtractPointFromPoint`方法
No method named subtractPointFromPoint found for type () Error in Rust Hey there! Let's break down this error you're seeing—it's a common gotcha when moving from languages like Python/JS to Rust, where expression vs statement behavior and method definitions work a bit differently.
First, let's unpack what the error means: the compiler is telling you that you tried to call subtractPointFromPoint on a value of type () (Rust's "unit type," which is like a void/empty value). That means wherever you're calling this method, the value you're calling it on isn't actually a Point instance—it's a blank, empty value instead.
Common Causes & Fixes
1. Your method definition is missing a return type, or isn't attached to the Point struct
Rust requires explicit method definitions inside an impl Point block, and you need to specify if the method returns a value (like a new Point). If your method is defined to return () (the default if you omit the return type), or if it's a standalone function instead of a method, you'll get this error.
Wrong way (returns () instead of Point):
// Inside point.rs struct Point { x: f32, y: f32, } impl Point { fn new(x: f32, y: f32) -> Self { Point { x, y } } // This method doesn't return anything—calling it gives you () fn subtractPointFromPoint(&self, other: &Point) { let _x = self.x - other.x; let _y = self.y - other.y; // No return statement, so returns () } }
Correct way (returns a new Point):
// Inside point.rs pub struct Point { pub x: f32, pub y: f32, } impl Point { pub fn new(x: f32, y: f32) -> Self { Point { x, y } } // Explicitly returns a Point, and is attached to the Point struct pub fn subtractPointFromPoint(&self, other: &Point) -> Point { Point { x: self.x - other.x, y: self.y - other.y, } } }
2. You accidentally turned a Point expression into a statement (extra semicolon!)
In Rust, lines with a semicolon are statements (they don't return a value—they return ()), while lines without a semicolon are expressions (they return their value). If you added an extra semicolon when creating a Point instance, you'll end up with () instead of a Point.
Wrong way (extra semicolon creates ()):
// Inside main.rs mod point; use point::Point; fn main() { // The semicolon turns this into a statement—p1 is now () let p1 = { Point::new(1.0, 2.0); }; let p2 = Point::new(3.0, 4.0); // Error: p1 is (), not Point p1.subtractPointFromPoint(&p2); }
Correct way (no semicolon, so returns Point):
// Inside main.rs mod point; use point::Point; fn main() { let p1 = Point::new(1.0, 2.0); let p2 = Point::new(3.0, 4.0); // Now p1 is a Point, so the method call works let result = p1.subtractPointFromPoint(&p2); println!("Result: x={}, y={}", result.x, result.y); }
3. You defined a standalone function instead of a method
If you wrote a regular function that takes two Points instead of a method attached to Point, calling it like p1.subtractPointFromPoint(&p2) will fail—because it's not a method on the Point type.
Wrong way (standalone function):
// Inside point.rs struct Point { x: f32, y: f32, } // This is a regular function, not a Point method fn subtractPointFromPoint(a: &Point, b: &Point) -> Point { Point { x: a.x - b.x, y: a.y - b.y } }
Fix: Either move it into an impl Point block (like the correct method example above), or call it as a regular function:
let result = subtractPointFromPoint(&p1, &p2);
Quick Rust Tip
While your camelCase method name works, Rust's standard convention is to use snake_case for functions and methods (e.g., subtract_point_from_point). It's not required, but it'll make your code feel more idiomatic to other Rust developers.
内容的提问来源于stack exchange,提问作者leon.stef

