Skip to content

Write down a Rust field's starting value when no constructor sets it [patch] - #229

Merged
matt-edmondson merged 1 commit into
mainfrom
fix/202-rust-field-default-note
Oct 14, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
fix/202-rust-field-default-note

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #202

What changed

If a field has an InitialValue and its type declares no constructor, the Rust generator used to write only name: type,. Nothing in the output showed the declared value had been dropped. Now it writes a note above the field, the same way Go and C already handle this case:

pub struct Counter {
    // count defaults to 0: Rust has no field initialisers, and no constructor sets it
    count: i32,
    // step defaults to 1: Rust has no field initialisers, and no constructor sets it
    step: i32,
}

A type that has a constructor gets no note. The constructor already builds any field it doesn't set from its declared value (#193).

The issue offered two options: a note, or a generated impl Default / fn new(). This PR takes the note option because it matches the Go and C output and doesn't make up any API. Generating Default would be a separate design change.

Tests

  • RustGeneratorTests.FieldInitialValue_WithoutAConstructor_IsANote: this test fails when the generator change is reverted and passes with it.
  • RustGeneratorTests.FieldInitialValue_WithAConstructor_IsNotANote
  • Full suite: 1282 passed, 0 failed (rustc and gcc were on the path).

docs/gallery/generated-rust.png shows the editor's sample Counter, so it will now be slightly out of date. I didn't regenerate it.

🤖 Generated with Claude Code

https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE


Generated by Claude Code

…[patch]

A field with an InitialValue in a type with no constructor was written as
`name: type,` with nothing to say its declared value had gone. Go and C
already write a "defaults to" note for the same case; Rust now does too.
A type with a constructor needs no note, because the constructor builds
a field it does not set from its declared value.

Fixes #202

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01LJsBos8vaJcor3uRqngfwE
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit 05d5646 into main Oct 14, 2026
16 checks passed
@matt-edmondson
matt-edmondson deleted the fix/202-rust-field-default-note branch October 14, 2026 09:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Rust silently drops an instance field's initial value when the type has no constructor (Go and C write a note)

2 participants