this post was submitted on 27 Dec 2024
13 points (71.0% liked)

Rust

6141 readers
48 users here now

Welcome to the Rust community! This is a place to discuss about the Rust programming language.

Wormhole

!performance@programming.dev

Credits

  • The icon is a modified version of the official rust logo (changing the colors to a gradient and black background)

founded 2 years ago
MODERATORS
 

Hello,

I learned about struct in Rust, so I just wanted to share what I have learned.

what is struct in Rust? It's the same as what's in C language. eg,

struct Point {
    x: i32,
    y: i32,
}

that's it this is how we define a struct. we can create all sort of struct with different data types. ( here I have used only i32 but you can use any data type you want)

now Rust also have which we find in OOPs languages like Java. it's called method. here is how we can define methods for a specific struct in Rust.

impl Point {
    fn print_point(&self) {
        println!("x: {} y: {}", self.x, self.y);
    }
}

see it's that easy. tell me if I forgot about something I should include about struct in Rust.

top 5 comments
sorted by: hot top controversial new old
[โ€“] 404@lemmy.zip 7 points 15 hours ago

You can implement Display for custom structs, to print them in the regular manner:

use std::fmt;

struct Point {
    x: i32,
    y: i32,
}

impl fmt::Display for Point {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "({}, {})", self.x, self.y)
    }
}

fn main() {
    let point = Point { x: 10, y: 20 };
    println!("{}", point); // using standard println!
}

You can also implement things like Add, Sub, AddAssign (point_a += point_b)... :)

[โ€“] Ephera@lemmy.ml 14 points 17 hours ago (1 children)

Maybe one thing to add, in addition to methods, you can also have associated functions, by leaving out the &self parameter:

impl Point {
    fn new(x: i32, y: i32) -> Point {
        Point { x, y }
    }
}

You call this function like so: Point::new(3, 5)

The example is quite deliberate. Most of the time, these associated functions are used for constructing the type itself, especially if you need to do more complex things for that than just initialize the struct. At that point, you don't have an object yet, so you couldn't be taking it as &self anyways. ๐Ÿ™ƒ

[โ€“] TehPers@beehaw.org 1 points 10 hours ago

In other languages, these are usually called static methods. Rust just uses these instead of constructor methods. That way you never have to work with a partially initialized value - you just create the value in your new function once you've initialized all its fields.

[โ€“] nous@programming.dev 6 points 16 hours ago

Structs are the AND data type in rust (ie it contains all the fields defined). As opposed to Enums which are the OR type (which can only be one of the variants defined). You also have ananomous data types like tuples or arrays.

The point about methods applies to any type in rust, structs are not unique or special there. Rust does not revolve around the struct type like other languages revolve around the class type.

[โ€“] itslilith@lemmy.blahaj.zone 4 points 17 hours ago

You put the basics pretty succinctly, I don't think I have anything to add! Take a look at traits next, and have your mind blown c: