Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add init-struct pattern #388

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft

Conversation

virtualritz
Copy link

See here for a more exhaustive description.

```rust
#[derive(Debug, Default, PartialEq)]
pub struct Foo {
pub foo: Some(u32)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub foo: Some(u32)
pub foo: Option<u32>,



#[test]
fn inint_struct_test() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn inint_struct_test() {
fn init_struct_test() {

fn inint_struct_test() {
let foo = Foo {
bar: "Some string".to_string(),
..Default::default
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
..Default::default
..Default::default()

@simonsan
Copy link
Collaborator

simonsan commented Jan 29, 2024

In general, I'm not sure, that this values an own article.

I also see it more as an idiom, than a pattern.

Can you look into https://github.com/rust-unofficial/patterns/blob/main/src/idioms/ctor.md and see what you would want to add there?

Also, there is something written down about that topic here already:

P.S.: Before writing an article and making a PR it's always better to open an issue/discussion before as stated in our Contribution guidelines, so there is no energy being wasted on either side.

@simonsan simonsan added C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content A-idiom Area: Idioms labels Jan 29, 2024
@simonsan simonsan marked this pull request as draft January 29, 2024 15:15
@virtualritz
Copy link
Author

In general, I'm not sure, that this values an own article.

I also see it more as an idiom, than a pattern.

I find that many Rustaceans, even people using the language for a while are oblibious of this pattern/idiom. Especially people coming from other languages. So I though rather than adding it as a "footnote" somehere on an existing page, I make a dedicated one.

Today I ran into this again here.
I would say overall I ran into people with considerable Rust experience being unaware of this maybe half a dozen times last year.

That's why I decided to write this. But it only took me 20mins. So if the PR is rejected, it's not a biggie. 😁

@simonsan
Copy link
Collaborator

So I though rather than adding it as a "footnote" somehere on an existing page, I make a dedicated one.

It doesn't need to be only a footnote in said existing article. You can write a few lines about it as well and then link the both links I posted for example, to make people aware, that this topic is explained in the book.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-idiom Area: Idioms C-needs discussion Area: Something that is not clear to everyone if it fixes something/adds valuable content
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants