Comments (5)
A few things.
- I agree that this is a factor, but calling it a constructor is probably reasonable too - though the term has some baggage.
- Agreed with 'Self'
- (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.
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.
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.
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.
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)
- Floating Point Related Idioms HOT 1
- Add double panic mentioning for RAII guards HOT 1
- Clarify parts of Lenses and Prisms chapter HOT 11
- Borrow Checker Patterns HOT 2
- Clarify paragraph in Generics as Type Classes
- Clarify naming convention of `build()` in section 3.2.1 HOT 1
- Design Patterns, Strategy has unclear identifier `Context` and `run` mentioned. HOT 1
- View type
- Translations HOT 6
- Incorrect text in Command pattern description
- Translation: Persian HOT 5
- Is this example wrong ? HOT 1
- Printing the book should not inherit theme colours HOT 1
- Extract code into runnable Rust files HOT 4
- Translate: Vietnamese HOT 2
- Typo in idioms/coercion-arguments.md HOT 3
- Add pdf renderer and link rendered book
- Consistent Headline Capitalization? HOT 1
- Incompatible definition of Mutex::lock (or MutexGuard)
- Foo Bar (ad nauseam)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from patterns.