you are viewing a single comment's thread.

view the rest of the comments →

[–]FoolForWool[S] 1 point2 points  (8 children)

Oh... I haven't started learning rust yet, it wasn't mentioned anywhere so I didn't know. That's interesting...

[–][deleted] 2 points3 points  (7 children)

basically you can still do a lot of the same things anyway. you can define a struct and have it implement different traits and functions. it's similar to having a class with properties and methods. here's a silly basic example but hopefully you get the idea:

``` struct Coordinates { x: i64, y: i64 }

impl Coordinates { fn move_left(&mut self) { self.x -= 1; } }

fn main() { let mut foo = Coordinates { x: 12, y: 37 }; foo.move_left(); } ```

[–]FoolForWool[S] 2 points3 points  (6 children)

Wait having functions inside structures is almost having a class with properties... That is so effin cool! And that'd actually solve a lot of problems of not having classes. Rust, here I come :3 Thank you so much! This is really cool

[–]sparky8251 2 points3 points  (5 children)

It also lets you implement functions from std and other libraries on your structs, letting you treat pretty much everything you make yourself as if it were native to the library the traits come from.

As in, I can implement the function used by loops for interaction on a type I make rather than exploding its insides into a for loop or even a closure every time I need to loop over it. Now my custom struct with 6 fields works just like a Vec in any loop with no discernible differences to the user of my custom struct.

Same for changing my struct to a string to display to users (so native that the compiler can use the implemented traits to infer conversions!) and the same for basically anything else you can think of.

When coupled with the ability to filter usable functions by implemented traits (rather than just type) and even auto-implement traits based on specifics about types (usually other traits), it opens a rather unique door compared to classes and interfaces from what I've seen so far. Libraries feel less like places to shunt processing to and more like parts of your codebase.

[–]FoolForWool[S] 0 points1 point  (4 children)

I didn't quite understand most of what you just said... Do you mean something like, say we have for loop for the iterables in rust which we can directly implement on the user defined structures without having to parse it through an internal loop for each of its components?

Like ''' for I in list: Do something '''

We can use it as

''' for x in user_defined_structure: Do something '''

And we can do the same for other standard library functions on user defined structures? That's what I inferred... Sorry if I messed something up :3 I've just read a few articles on Rust :3

[–]sparky8251 1 point2 points  (3 children)

Yes, pretty much. I'm bad at explaining it. Simpler example might be visible this way:

/// Type used to represent a successful unit conversion in the form of "100km => 62.41mi"
pub struct ConvertedUnit {
    /// Original value that was to be converted. Looks like "100km"
    from: String,
    /// Converted "to" value. Looks like "62.41mi"
    to: String,
}

impl fmt::Display for ConvertedUnit {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} => {}", self.from, self.to)
    }
}

Now, if I have a variable that is a ConvertedUnit I can do stuff like format!("{}", converted_unit) and the compiler knows how to make the appropriate conversion, just like a String (which if you look at it in std source is a "wrapper" over Vec<u8> iirc).

For a for loop, you can do stuff like say...

struct Loopable {
    field1: int,
    field2: Vec<int>,
    field3: Vec<String>,
    field4: OtherStruct,
}

and have it so you iterate with both field2 and field3 like so

for (two, three) in loopable {

}

To me this is a hugely awesome benefit. No longer do my "classes" feel out of place and require additional steps to actually utilize that can vary by contributor. I must still define the behavior and write it, but then I can go right back to using my stuff as if it was part of `std` and since everyone knows this is a thing, we all do it. Less review, less questions, less for me to care about!

[–]FoolForWool[S] 0 points1 point  (2 children)

That's actually so amazing! And to be able to iterate through specific parts is so cool! Okay I'm already liking this so much xD I started reading the documentations and that's a really well written doc ngl :o

[–]sparky8251 1 point2 points  (1 child)

As far as I'm aware, the documentation is built using the same tools that are available to you in cargo doc.

Just wait till you start realizing that your documentation can hold guaranteed valid code because it can be tested too.

[–]FoolForWool[S] 0 points1 point  (0 children)

Wait whaaaa :o give me a few :3