Skip to content

I would like to know how to create struct and enum.. #3483

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ghost opened this issue Sep 13, 2012 · 2 comments
Closed

I would like to know how to create struct and enum.. #3483

ghost opened this issue Sep 13, 2012 · 2 comments

Comments

@ghost
Copy link

ghost commented Sep 13, 2012

import io::*;

struct Point {x:float, y:float}

enum Shape{
    Circle(Point, float),
    Rectangle(Point, Point)
}

fn area(sh: Shape)->float{


    match sh{
        Circle(_,size)=>float::consts::pi*size*size,

        Rectangle(point1, point2)=>(point2.x-point1.x)*(point2.y-point1.y)
    }
}

fn main(){

    Circle{(4.5,5.5,),1.0};

    area(circle);

}

it doesn't work.. I'm a person who is using english as second language..
So there's a problem to understand document..

Please Let me know how to use it properly

@killerswan
Copy link
Contributor

As we talked about on IRC, this was an example that helped (I'm using version 0.4dev):

use std;
use io::*;

struct Point {x:float, y:float}

enum Shape{
   Circle(Point, float),
   Rectangle(Point, Point)
}

fn area(sh: Shape) -> float {
   match sh {
      Circle(_,size) => float::consts::pi*size*size,
      Rectangle(point1, point2) => (point2.x-point1.x)*(point2.y-point1.y)
   }
}

fn main() {
   let pt: Point = Point {x:4.5, y:5.5};
   let circle: Shape = Circle(pt,1.7);
   area(circle);

   let pi = float::consts::pi;

   if area(circle) - 1.7 * 1.7 * pi < 0.1  { io::println("OK"); }
}

@catamorphism
Copy link
Contributor

Closing this; no clear bug being reported.

bors pushed a commit to rust-lang-ci/rust that referenced this issue May 15, 2021
Enable overflow_delimited_expr for structs
RalfJung pushed a commit to RalfJung/rust that referenced this issue Apr 20, 2024
add test for Drop terminator on non-drop type

Fixes rust-lang/miri#2737
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants