Rust—结构体

结构体

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//定义结构体Student<br>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.特殊结构体之元组结构体

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

    如定义颜色结构体:    

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

     定义坐标结构体:

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

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

    无任何成员

    如struct just_unit_struct;

  c.整个结构体输出    

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#[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.结构体方法

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const pi:f64 = 3.141592f64;
 
#[derive(Debug)]
struct Point(f64,f64);
 
#[derive(Debug)]
struct Circle{
    Radius:f64,
    center_point:Point,
}<br>//结构体方法定义,拥有&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.结构体关联函数

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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());
}

 运行结果:

 

 

 

 

 

 

 

 

 

 

    

      

    

 

posted @   echo-efun  阅读(90)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· 没有源码,如何修改代码逻辑?
· PowerShell开发游戏 · 打蜜蜂
· 在鹅厂做java开发是什么体验
· WPF到Web的无缝过渡:英雄联盟客户端的OpenSilver迁移实战
点击右上角即可分享
微信分享提示