Return the last element of an enum as Default in Rust2019 Community Moderator ElectionHow do I exit a Rust program early from outside the main function?Why doesn't println! work in Rust unit tests?Allocating an object for C / FFI library callsHow to return reference to locally allocated struct/object? AKA error: `foo` does not live long enoughTrait to store structs with different generic parametersHow do you make a non-hashable, C-like enum from a library hashable?Class subtyping in RustClone for all-Clone structures with a non-Clone type parameterHow do I create a polymorphic array and then convert a value to the concrete type?Ownership and lifetime in nested iterators in rust for string comparison
How do we create new idioms and use them in a novel?
Use Mercury as quenching liquid for swords?
When to use a QR code on a business card?
Automaton recognizing ambiguously accepted words of another automaton
Boss Telling direct supervisor I snitched
Can I negotiate a patent idea for a raise, under French law?
What is the purpose of a disclaimer like "this is not legal advice"?
If nine coins are tossed, what is the probability that the number of heads is even?
Why do phishing e-mails use faked e-mail addresses instead of the real one?
Short scifi story where reproductive organs are converted to produce "materials", pregnant protagonist is "found fit" to be a mother
Yet another question on sums of the reciprocals of the primes
Was it really inappropriate to write a pull request for the company I interviewed with?
What happened to the colonial estates belonging to loyalists after the American Revolution?
Would those living in a "perfect society" not understand satire
Having the player face themselves after the mid-game
How to install round brake pads
Rationale to prefer local variables over instance variables?
What does *dead* mean in *What do you mean, dead?*?
What is this tube in a jet engine's air intake?
If sound is a longitudinal wave, why can we hear it if our ears aren't aligned with the propagation direction?
Does an unused member variable take up memory?
Is "cogitate" used appropriately in "I cogitate that success relies on hard work"?
School performs periodic password audits. Is my password compromised?
Either of .... (Plural/Singular)
Return the last element of an enum as Default in Rust
2019 Community Moderator ElectionHow do I exit a Rust program early from outside the main function?Why doesn't println! work in Rust unit tests?Allocating an object for C / FFI library callsHow to return reference to locally allocated struct/object? AKA error: `foo` does not live long enoughTrait to store structs with different generic parametersHow do you make a non-hashable, C-like enum from a library hashable?Class subtyping in RustClone for all-Clone structures with a non-Clone type parameterHow do I create a polymorphic array and then convert a value to the concrete type?Ownership and lifetime in nested iterators in rust for string comparison
I have a rust implementation as follows and in default implementation, for the enum, I need to return the last element and I need to achieve it without hardcoding it.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Region<CountryId>
None,
Category(CountryId),
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature1<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature2<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
pub Work: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Alphabets<CountryId>
A1(Litrature1<CountryId>),
A2(Litrature1<CountryId>)
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
// How to return the last element of the enum as default?
Alphabets<CountryId>::A2
Playground
I am uncertain how to make this work
rust
add a comment |
I have a rust implementation as follows and in default implementation, for the enum, I need to return the last element and I need to achieve it without hardcoding it.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Region<CountryId>
None,
Category(CountryId),
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature1<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature2<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
pub Work: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Alphabets<CountryId>
A1(Litrature1<CountryId>),
A2(Litrature1<CountryId>)
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
// How to return the last element of the enum as default?
Alphabets<CountryId>::A2
Playground
I am uncertain how to make this work
rust
YourAlphabets
enum variants each wrap around aLitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 withRegion::None
forSeek
andWrite
?
– loganfsmyth
Mar 6 at 23:06
Yes , thats correct
– co2f2e
Mar 7 at 2:20
add a comment |
I have a rust implementation as follows and in default implementation, for the enum, I need to return the last element and I need to achieve it without hardcoding it.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Region<CountryId>
None,
Category(CountryId),
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature1<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature2<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
pub Work: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Alphabets<CountryId>
A1(Litrature1<CountryId>),
A2(Litrature1<CountryId>)
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
// How to return the last element of the enum as default?
Alphabets<CountryId>::A2
Playground
I am uncertain how to make this work
rust
I have a rust implementation as follows and in default implementation, for the enum, I need to return the last element and I need to achieve it without hardcoding it.
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Region<CountryId>
None,
Category(CountryId),
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature1<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub struct Litrature2<CountryId>
pub Seek: Region<CountryId>,
pub Write: Region<CountryId>,
pub Work: Region<CountryId>,
#[cfg_attr(feature = "std", derive(Debug))]
#[derive(Clone, PartialEq, Eq)]
pub enum Alphabets<CountryId>
A1(Litrature1<CountryId>),
A2(Litrature1<CountryId>)
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
// How to return the last element of the enum as default?
Alphabets<CountryId>::A2
Playground
I am uncertain how to make this work
rust
rust
edited 2 days ago
Akiner Alkan
1,518325
1,518325
asked Mar 6 at 22:21
co2f2eco2f2e
5,984134989
5,984134989
YourAlphabets
enum variants each wrap around aLitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 withRegion::None
forSeek
andWrite
?
– loganfsmyth
Mar 6 at 23:06
Yes , thats correct
– co2f2e
Mar 7 at 2:20
add a comment |
YourAlphabets
enum variants each wrap around aLitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 withRegion::None
forSeek
andWrite
?
– loganfsmyth
Mar 6 at 23:06
Yes , thats correct
– co2f2e
Mar 7 at 2:20
Your
Alphabets
enum variants each wrap around a LitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 with Region::None
for Seek
and Write
?– loganfsmyth
Mar 6 at 23:06
Your
Alphabets
enum variants each wrap around a LitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 with Region::None
for Seek
and Write
?– loganfsmyth
Mar 6 at 23:06
Yes , thats correct
– co2f2e
Mar 7 at 2:20
Yes , thats correct
– co2f2e
Mar 7 at 2:20
add a comment |
1 Answer
1
active
oldest
votes
I'm assuming that you essentially want default values all the way down, where each region defaults to Region::None
. In that context, it would make the most sense to define Default
on each nested type, e.g.
Default for Region
impl<CountryId> Default for Region<CountryId>
fn default() -> Self
Region::None
Default for Litrature1
impl<CountryId> Default for Litrature1<CountryId>
fn default() -> Self
Litrature1
Seek: Default::default(),
Write: Default::default(),
Default for Litrature2
impl<CountryId> Default for Litrature2<CountryId>
fn default() -> Self
Litrature2
Seek: Default::default(),
Write: Default::default(),
Work: Default::default(),
Default for Alphabets
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
Alphabets::A2(Default::default())
(On the Rust playground)
Or you could just deriveDefault
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
@mcarton Unfortunately that would requireCountryId
to implementDefault
because of github.com/rust-lang/rust/issues/26925.
– loganfsmyth
2 days ago
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033113%2freturn-the-last-element-of-an-enum-as-default-in-rust%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
I'm assuming that you essentially want default values all the way down, where each region defaults to Region::None
. In that context, it would make the most sense to define Default
on each nested type, e.g.
Default for Region
impl<CountryId> Default for Region<CountryId>
fn default() -> Self
Region::None
Default for Litrature1
impl<CountryId> Default for Litrature1<CountryId>
fn default() -> Self
Litrature1
Seek: Default::default(),
Write: Default::default(),
Default for Litrature2
impl<CountryId> Default for Litrature2<CountryId>
fn default() -> Self
Litrature2
Seek: Default::default(),
Write: Default::default(),
Work: Default::default(),
Default for Alphabets
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
Alphabets::A2(Default::default())
(On the Rust playground)
Or you could just deriveDefault
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
@mcarton Unfortunately that would requireCountryId
to implementDefault
because of github.com/rust-lang/rust/issues/26925.
– loganfsmyth
2 days ago
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
add a comment |
I'm assuming that you essentially want default values all the way down, where each region defaults to Region::None
. In that context, it would make the most sense to define Default
on each nested type, e.g.
Default for Region
impl<CountryId> Default for Region<CountryId>
fn default() -> Self
Region::None
Default for Litrature1
impl<CountryId> Default for Litrature1<CountryId>
fn default() -> Self
Litrature1
Seek: Default::default(),
Write: Default::default(),
Default for Litrature2
impl<CountryId> Default for Litrature2<CountryId>
fn default() -> Self
Litrature2
Seek: Default::default(),
Write: Default::default(),
Work: Default::default(),
Default for Alphabets
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
Alphabets::A2(Default::default())
(On the Rust playground)
Or you could just deriveDefault
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
@mcarton Unfortunately that would requireCountryId
to implementDefault
because of github.com/rust-lang/rust/issues/26925.
– loganfsmyth
2 days ago
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
add a comment |
I'm assuming that you essentially want default values all the way down, where each region defaults to Region::None
. In that context, it would make the most sense to define Default
on each nested type, e.g.
Default for Region
impl<CountryId> Default for Region<CountryId>
fn default() -> Self
Region::None
Default for Litrature1
impl<CountryId> Default for Litrature1<CountryId>
fn default() -> Self
Litrature1
Seek: Default::default(),
Write: Default::default(),
Default for Litrature2
impl<CountryId> Default for Litrature2<CountryId>
fn default() -> Self
Litrature2
Seek: Default::default(),
Write: Default::default(),
Work: Default::default(),
Default for Alphabets
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
Alphabets::A2(Default::default())
(On the Rust playground)
I'm assuming that you essentially want default values all the way down, where each region defaults to Region::None
. In that context, it would make the most sense to define Default
on each nested type, e.g.
Default for Region
impl<CountryId> Default for Region<CountryId>
fn default() -> Self
Region::None
Default for Litrature1
impl<CountryId> Default for Litrature1<CountryId>
fn default() -> Self
Litrature1
Seek: Default::default(),
Write: Default::default(),
Default for Litrature2
impl<CountryId> Default for Litrature2<CountryId>
fn default() -> Self
Litrature2
Seek: Default::default(),
Write: Default::default(),
Work: Default::default(),
Default for Alphabets
impl<CountryId> Default for Alphabets<CountryId>
fn default() -> Self
Alphabets::A2(Default::default())
(On the Rust playground)
answered Mar 6 at 23:12
loganfsmythloganfsmyth
105k21223187
105k21223187
Or you could just deriveDefault
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
@mcarton Unfortunately that would requireCountryId
to implementDefault
because of github.com/rust-lang/rust/issues/26925.
– loganfsmyth
2 days ago
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
add a comment |
Or you could just deriveDefault
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
@mcarton Unfortunately that would requireCountryId
to implementDefault
because of github.com/rust-lang/rust/issues/26925.
– loganfsmyth
2 days ago
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
Or you could just derive
Default
– mcarton
Mar 7 at 1:25
Or you could just derive
Default
– mcarton
Mar 7 at 1:25
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
@loganfsmyth Rather than Hardcoding the last item as ::A2 Im looking for a dynamic way to get the last item Thanks
– co2f2e
Mar 7 at 2:18
1
1
@mcarton Unfortunately that would require
CountryId
to implement Default
because of github.com/rust-lang/rust/issues/26925.– loganfsmyth
2 days ago
@mcarton Unfortunately that would require
CountryId
to implement Default
because of github.com/rust-lang/rust/issues/26925.– loganfsmyth
2 days ago
2
2
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
@co2f2e Would something like github.com/idanarye/rust-smart-default work? There's nothing special about the last variant of an enum, so it's it's not something that would be built in.
– loganfsmyth
2 days ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55033113%2freturn-the-last-element-of-an-enum-as-default-in-rust%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Your
Alphabets
enum variants each wrap around aLitratureX
value, so what should the value of that be for your default implementation? Should it be a Literature1 withRegion::None
forSeek
andWrite
?– loganfsmyth
Mar 6 at 23:06
Yes , thats correct
– co2f2e
Mar 7 at 2:20