# Default attribute for string fields

**URL:** <https://users.rust-lang.org/t/default-attribute-for-string-fields/113719>\
**Category:** uncategorized\
**Created:** [July 1, 2024, 11:14am UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719 "2024-07-01T11:14:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amab8901](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/amab8901/32/41652_2.png) [@amab8901](https://users.rust-lang.org/u/amab8901)\
**Post date:** [July 1, 2024, 11:14am UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719/1 "2024-07-01T11:14:02Z")

</div>

my code:

```rust
fn main() {}

#[derive(Default)]
pub struct Structer {
    #[default("ok")]
    pub stringy: String,
}

```

produces this error:

```rust
the `#[default]` attribute may only be used on unit enum variants
consider a manual implementation of `Default`rustcClick for full compiler diagnostic

```

Can u implement this feature?

I think it shouldn't be that hard to impl it. I imagine it would go something like this:

1. read the token inserted in the default attribute (in this case `"ok"`)
2. check the type of the token (in this case `&str`)
3. check the required type of the field
4. append ".to\_string() to the token
5. use the modified token as field value like this:

```rust
fn default(modified_token: &str) -> String {
    let default = Structer {
        stringy: modified_token.to_string()
    };
    default
}

```

---

<div class="post-metadata">

**Author:** ![Cerber-Ursi](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/cerber-ursi/32/8315_2.png) [@Cerber-Ursi](https://users.rust-lang.org/u/Cerber-Ursi)\
**Post date:** [July 1, 2024, 11:20am UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719/2 "2024-07-01T11:20:39Z")

</div>

This isn't something that is implemented by the standard library. You might want to check [derivative](https://lib.rs/crates/derivative) as an extension of standard `derive`s.

---

<div class="post-metadata">

**Author:** ![amab8901](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/amab8901/32/41652_2.png) [@amab8901](https://users.rust-lang.org/u/amab8901)\
**Post date:** [July 1, 2024, 11:22am UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719/3 "2024-07-01T11:22:20Z")

</div>

I tried it but `derivative` doesn't seem to support strings (although it does support integers etc). Or maybe I just used the wrong approach

---

<div class="post-metadata">

**Author:** ![jofas](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/jofas/32/30690_2.png) [@jofas](https://users.rust-lang.org/u/jofas)\
**Post date:** [July 1, 2024, 1:09pm UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719/4 "2024-07-01T13:09:11Z")

</div>

It's a bit more tricky to use `derivative` with strings, but it is doable:

```rust
use derivative::Derivative; 

#[derive(Derivative)]
#[derivative(Default)]
pub struct Structer {
    #[derivative(Default(value=r#""ok".to_owned()"#))]
    pub stringy: String,
}

fn main() {
    assert_eq!(Structer::default().stringy, "ok".to_owned());
}

```

[Playground.](https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=f5c633e12f1081da6d5cbd0ef2d668c0)

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [September 29, 2024, 1:09pm UTC](https://users.rust-lang.org/t/default-attribute-for-string-fields/113719/5 "2024-09-29T13:09:33Z")

</div>

Closed on September 29, 2024, 1:09pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
