Code Monkey home page Code Monkey logo

Comments (5)

insanitybit avatar insanitybit commented on May 14, 2024 1

A few things.

  1. I agree that this is a factor, but calling it a constructor is probably reasonable too - though the term has some baggage.
  2. Agreed with 'Self'
  3. (why I came to issues) I think there should be a note about the Default trait - if you implement 'new' with no parameters you should also implement Default.

from patterns.

yuriykulikov avatar yuriykulikov commented on May 14, 2024

Hey all,
I am new to Rust I and I am a little bit confused why this is called a constructor and not a factory function. Does it make sense to rename this?

from patterns.

joseluis avatar joseluis commented on May 14, 2024

I personally prefer it like this:

    pub fn new() -> Self {
        // Create a new Vec with fields properly initialised.
        Vec {
            // Note that here we are calling RawVec's constructor.
            buf: RawVec::new(),
            len: 0,
        }
    }

The Self clearly indicates it's a constructor, and the Vec quickly reminds for which type.
It helps when you are implementing for multiple related types in the same file.

from patterns.

MarcoIeni avatar MarcoIeni commented on May 14, 2024

Does it make sense to rename this?

Constructor is the name commonly used in rustlang for this from what I see around. It is common in cpp, too.

I think there should be a note about the Default trait

Definitely, PR well accepted!

Anyway, Self was discussed in #61, too. My point is that it might be better to avoid it in book examples in order to make them clearer. However I extensively use it when I code and a PR where you add a note about the use of Self in the constructor page is well accepted if you want.
You could also link to #145 once it is done.

  • add note about the Default trait.

from patterns.

pickfire avatar pickfire commented on May 14, 2024

Anyway, Self was discussed in #61, too. My point is that it might be better to avoid it in book examples in order to make them clearer. However I extensively use it when I code and a PR where you add a note about the use of Self in the constructor page is well accepted if you want.

On the other hand, I never use it unless the type is too complex (multiple generics and lifetimes). Using Self makes the rendered doc harder to figure out what is the type (there is only a word Self) and by looking at the code itself you don't get much context, I still prefer it to be more explicit by just typing out the original type.

People coming from another language may think there can only be one constructor, but in rust you can have multiple. I think the correct term is called static method (and the rest being known as instance methods). https://doc.rust-lang.org/rust-by-example/fn/methods.html

from patterns.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.