Struct study in day3

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

struct Student {
    id: String,
    name: String,
    age: u8,
    sex: char,
    height: f32,
    weight: f32,

// 元组结构体 字段无名称结构体
struct Point(i32,i32,i32);

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

struct Cylinder{

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();

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{
    // 结构体更新语法,会使用到变量与数据的移动特性
    let stu3 = Student {
        name: String::from("heitieyihui"),
    //println!("stu1 = {:?}", stu1);//报错
    // 元组结构体
    let p1 = Point(3,1,2);
    println!("p1 = {:?}", p1);

// 计算圆柱表面积
fn make_struct_demo(){
    let cyl = Cylinder{
    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


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


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):





  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文件中 ↩︎


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 语言设计者来说,了解用户的实际需求也可能很有用。



I mean he can post on tutorials,no other 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.