其他分享
首页 > 其他分享> > Rust—结构体

Rust—结构体

作者:互联网

结构体

  

//定义结构体Student
struct Student{ name:&'static str, score:i32, } fn main(){ let score = 59; let username ="zhangsan"; let mut student = Student{ score, name:username, }; student.score = 60; println!("name:{},score:{}",student.name,student.score); let student2 = Student{ name:"lisi", ..student //有别于其他编程语言地方 }; println!("name:{},score:{}",student2.name,student2.score); }

 输出结果:

  

 

  a.特殊结构体之元组结构体

    只有字段类型,没有字段名称的特殊结构体

    如定义颜色结构体:    

struct Color(i32,i32,i32);
let balck = Color(0,0,0);

     定义坐标结构体:

struct Site(f64,f64,f64);
let location = Site(1.0,1.0,1.0);

  b.特殊结构体之单元结构体

    无任何成员

    如struct just_unit_struct;

  c.整个结构体输出    

#[derive(Debug)]   //{:?}使用必需语句
struct Student{
    name:&'static str,
    score:i32,
}
fn main(){
    let score = 59;
    let username ="zhangsan";

    let mut student = Student{
        score,
        name:username,
    };

    student.score = 60;
    println!("name:{},score:{}",student.name,student.score);

    let student2 = Student{
        name:"lisi",
        ..student
    };
    println!("name:{},score:{}",student2.name,student2.score);


    //输出结构体student
    println!("struct student is {:?}",&student);
}

     输出结果:

   d.结构体方法

 

const pi:f64 = 3.141592f64;

#[derive(Debug)]
struct Point(f64,f64);

#[derive(Debug)]
struct Circle{
    Radius:f64,
    center_point:Point,
}
//结构体方法定义,拥有&self参数 impl Circle{ fn area(&self)->f64{ self.Radius*self.Radius*pi } } fn main(){ let radius1:f64 = 1f64; let point1= Point(2f64,2f64); //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处OK let circle1 = Circle{ Radius:radius1, center_point:point1, }; //println!("circle center point is ({},{})",point1.0,point1.1); //放在此处会报错,point1已经不存在了 println!("circle center point is ({},{})",circle1.center_point.0,circle1.center_point.1); println!("{:?} area is {}",circle1,circle1.area()); }

      输出结果:

 

  e.结构体关联函数

  

const pi:f64 = 3.141592f64;

#[derive(Debug)]
struct Point(f64,f64);
///结构体关联函数
impl Point{
    fn create(x:f64,y:f64)->Point{
        Point(x,y)
    }
}

#[derive(Debug)]
struct Circle{
    Radius:f64,
    center_point:Point,
}
///结构体方法
impl Circle{
    fn area(&self)->f64{
        self.Radius*self.Radius*pi
    }
}

fn main(){
    let radius1:f64 = 1f64;
    let point1= Point(2f64,2f64);
    let point2 = Point::create(1.0, 2.0);
    //println!("circle center point is ({},{})",point1.0,point1.1);  //放在此处OK
    let circle1 = Circle{
        Radius:radius1,
        center_point:point1,

    };
    //println!("circle center point is ({},{})",point1.0,point1.1);    //放在此处会报错,point1已经不存在了
    println!("circle center point1 is ({},{})",circle1.center_point.0,circle1.center_point.1);   
    println!("point2 is ({},{})",point2.0,point2.1);
    println!("{:?} area is {}",circle1,circle1.area());
}

 运行结果:

 

 

 

 

 

 

 

 

 

 

    

      

    

 

标签:name,center,score,let,println,f64,Rust,结构
来源: https://www.cnblogs.com/echo-efun/p/15768128.html