Struct study in day3

#![crate_type = "cdylib"]
const PI:f32 = 3.1415926;

#[derive(Debug)]
struct Student {
    id: String,
    name: String,
    age: u8,
    sex: char,
    height: f32,
    weight: f32,
}

// 元组结构体 字段无名称结构体
#[derive(Debug)]
struct Point(i32,i32,i32);

// 类单元结构体 无字段结构体
struct Body;

struct Cylinder{
    radius:f32,
    tall:f32,
}

impl Cylinder{
    fn get_basal_area(&self)->f32{
        PI * self.radius * self.radius
    }
    
    fn get_lateral_area(&self)->f32{
        2.0 * PI * self.radius * self.tall
    }
    
    fn get_superficial_area(&self)->f32{
        self.get_lateral_area() + (2.0 * self.get_basal_area())
    }
}

fn main() {
    // struct_test();
    make_struct_demo();
}

fn struct_test() {

    let mut stu1 = Student {
        id: String::from("10823695"),
        name: String::from("heitiejingjie"),
        age: 25,
        sex: '男',
        height: 175.5,
        weight: 67.5,
    };
    println!("stul = {:#?}", stu1);
    
    stu1.height = 175.0;
    println!("height = {}", stu1.height);
    
    // 字段初始化简写 
    let name = String::from("jiushiwo");
    let id = String::from("1083695");
    let stu2 = Student{
        id:id,
        name:name,
        age:27,
        sex:'男',
        height:170.0,
        weight:65.0
    };
    
    // 结构体更新语法,会使用到变量与数据的移动特性
    let stu3 = Student {
        name: String::from("heitieyihui"),
        ..stu1
    };
    //println!("stu1 = {:?}", stu1);//报错
    
    // 元组结构体
    let p1 = Point(3,1,2);
    println!("p1 = {:?}", p1);
}

// 计算圆柱表面积
fn make_struct_demo(){
    let cyl = Cylinder{
        radius:3.6,
        tall:9.8
    };
    
    let super_area = get_lateral_area(&cyl) + 2.0 * (PI * cyl.radius * cyl.radius);
    
    println!("super_area = {}", super_area);
    
    let area = cyl.get_superficial_area();
    println!("{}", area);
}

fn get_lateral_area(cyl:&Cylinder)->f32{

    2.0 * PI * cyl.radius * cyl.tall
}

(Playground)

Yep, that's some Rust code, alright. Do you have a question?

3 Likes

no,that is record

1 Like

好像这里不适合发学习的东西 aha

Generally, the function of sharing code in this forum is used for asking questions or feedback. Sharing tutorials or similar material is also possible. If you’re planning to write down your Rust learning journey in the form of code with comments, I think that’s a great approach! If you want to leave these notes for others to learn from, or give feedback to, that’s fine, too; there’s even a tutorials category on this forum.

However, for future posts, we would appreciate if you don’t keep open a new topic for each code snippet, as that could be perceived by our forum users as being a bit too spammy. You could just as well accumulate the code snippets and playground links in a single topic, and even post multiple code shares / playground links cumulatively, not necessarily every day but say after a whole week, maybe even after some reviewing them. Alternatively, you could for example also set up a GitHub repository where you add your study notes in the form of .rs files or maybe .md pages[1], or as cargo project directories, depending on whether you place value on it being neat to read online or easy to execute for people downloading the project, etc…

Hosting your code on GitHub like that would allow you to share your GitHub as a “tutorial” source in the forum only once, and then whoever is interested can follow and always have a good overview.

In any case, I wish you good luck and lots of fun on your further Rust learning journey, and beyond just recording your journey, do not hesitate to come back here with any questions about Rust you need answered.


以下为机器翻译内容 (Machine Translated Content):

通常,在本论坛中分享代码的功能用于提问或寻求反馈。分享教程或类似材料也是可能的。如果你计划以代码和注释的形式记录你的Rust学习之旅,我认为这是一个很好的方法!如果你想留下这些笔记供他人学习或反馈,这也是可以的;我们论坛甚至有一个名为“tutorials”(教程)的类别

然而,对于未来的帖子,我们希望您不要为每个代码片段开设一个新话题,因为这可能会被我们的论坛用户视为有点垃圾邮件。您可以在一个主题中累积代码片段和游乐场链接,甚至可以累积地发布多个代码分享/游乐场链接,不一定是每天,但比如说一周后,甚至可能在某些复查之后。或者,您也可以例如设置一个GitHub仓库,在那里添加您的学习笔记,形式可以是.rs文件或也许是.md页面[2],或者作为cargo项目目录,这取决于您是更重视在线阅读的整洁度还是下载项目的人更容易执行等等…

像这样在GitHub上托管您的代码将允许您在论坛上只分享一次您的GitHub作为“教程”来源,然后任何感兴趣的人都可以跟进并始终有一个很好的概览。

无论如何,我祝您在Rust学习之旅中好运和乐趣,并且不仅仅是记录您的旅程,请随时回到这里,解答您对Rust的任何疑问。


  1. which can also contain links to Rust Playground shares; for example you could literally copy-paste the generated template from the “Open a new thread in the Rust user forum” button, into a markdown file ↩︎

  2. 这也可以包含到Rust Playground分享的链接;例如,您可以直接从“Open a new thread in the Rust user forum”(在Rust用户论坛开设新话题)的按钮生成的模板复制粘贴到markdown文件中 ↩︎

7 Likes

I do hope you are joking. The people on this forum are incredibly helpful for those who want to learn.

On the contrary, posting some random code snippets here with no comment or question is itself not helpful for anyone learning.

I suggest that if one is learning Rust from scratch it could actually be useful to others to post what one has learned here, day by day or little feature by little feature. But it would need some supporting information along the way. Like:

What programming languages / background is one starting from.

Say what one found obvious or easy.

Say what one found surprising or difficult or did not understand what features were actually for.

Point out anything confusing / wrong in the material one is learning from. Especially the Rust Book.

Put all this, post by post in a single thread so that the thread tells a coherent story of progress.

Such a work might be useful to others who are learning along with you. It might also be useful for those writing the Rust teaching materials, they might have things to make clearer in their work. It might also be useful to the Rust language designers to see what users actually need.

Good luck.

我真希望你是在开玩笑。 这个论坛上的人对那些想学习的人非常有帮助。

相反,在这里发布一些没有评论或问题的随机代码片段本身对任何人的学习都没有帮助。

我建议,如果一个人从头开始学习 Rust,那么将一个人在这里学到的东西(一天一天或一个功能一个功能地)发布出来实际上对其他人来说可能很有用。 但在此过程中需要一些支持信息。 喜欢:

从什么编程语言/背景开始。

说出人们认为显而易见或容易的事情。

说出人们感到惊讶或困难或不明白功能实际用途的内容。

指出正在学习的材料中任何令人困惑/错误的地方。 尤其是《锈书》。

将所有这些内容逐个帖子地放在一个线程中,以便该线程讲述一个连贯的进展故事。

这样的工作可能对与你一起学习的其他人有用。 它对于那些编写 Rust 教材的人来说也可能有用,他们可能在工作中需要澄清一些事情。 对于 Rust 语言设计者来说,了解用户的实际需求也可能很有用。

祝你好运。

5 Likes

I mean he can post on tutorials,no other meaning.so i used chinese to told him.cuz i saw one guy asked him if he need help.but also thx for raminding me!

okok,sorry,I will push github.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.