Open Amjad50 opened 4 years ago
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?
A few things.
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.
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.
Default
trait. 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
I'm used to use
Self
for the class type insideimpl
from:
to:
this makes it easier in case of renaming etc., what do you think?