Compile error: expected type `Rectangle`, found type `square::Rectangle`

I want this code function to return Rectangle, but can't understand how.

fn square(rect_point: Point,sqr: f32) -> Rectangle{


    struct Rectangle {

        width: f32,

        height: f32,

        bottom_left: Point


    return Rectangle{ width: sqr, height: sqr, bottom_left: rect_point};



    struct Point {

    x: f32,

    y: f32


fn main() {
    let point: Point = Point{ x: 3.14,y: 0.3};
    //println!("The area of a square is = {:?}",square(point, 10.2));

Can you please explain what am doing wrong?

It's impossible to name the structure defined in the function outside its body. So, when you specify the return type as Rectangle, this is not the Rectangle you've defined here, this is some other Rectangle defined elsewhere.

That means i should create the structure outside the function and reference it?

You should define the structure outside the function - i.e. the struct Rectangle { ... } part must be somewhere outside to be accessible. Of course, creating the struct - the return Rectangle { ... } statement - will stay in the function, since this seems to be your intent.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.