ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Rust—结构体

2022-01-06 10:04:06  阅读:179  来源: 互联网

标签:name center score let println f64 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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有