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










0















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










share|improve this question
























  • 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















0















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










share|improve this question
























  • 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













0












0








0








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










share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









Akiner Alkan

1,518325




1,518325










asked Mar 6 at 22:21









co2f2eco2f2e

5,984134989




5,984134989












  • 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

















  • 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
















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












1 Answer
1






active

oldest

votes


















1














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)






share|improve this answer























  • 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






  • 1





    @mcarton Unfortunately that would require CountryId to implement Default 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










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
);



);













draft saved

draft discarded


















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









1














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)






share|improve this answer























  • 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






  • 1





    @mcarton Unfortunately that would require CountryId to implement Default 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















1














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)






share|improve this answer























  • 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






  • 1





    @mcarton Unfortunately that would require CountryId to implement Default 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













1












1








1







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)






share|improve this answer













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)







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 23:12









loganfsmythloganfsmyth

105k21223187




105k21223187












  • 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






  • 1





    @mcarton Unfortunately that would require CountryId to implement Default 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











  • @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 require CountryId to implement Default 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Can't initialize raids on a new ASUS Prime B360M-A motherboard2019 Community Moderator ElectionSimilar to RAID config yet more like mirroring solution?Can't get motherboard serial numberWhy does the BIOS entry point start with a WBINVD instruction?UEFI performance Asus Maximus V Extreme

Identity Server 4 is not redirecting to Angular app after login2019 Community Moderator ElectionIdentity Server 4 and dockerIdentityserver implicit flow unauthorized_clientIdentityServer Hybrid Flow - Access Token is null after user successful loginIdentity Server to MVC client : Page Redirect After loginLogin with Steam OpenId(oidc-client-js)Identity Server 4+.NET Core 2.0 + IdentityIdentityServer4 post-login redirect not working in Edge browserCall to IdentityServer4 generates System.NullReferenceException: Object reference not set to an instance of an objectIdentityServer4 without HTTPS not workingHow to get Authorization code from identity server without login form

2005 Ahvaz unrest Contents Background Causes Casualties Aftermath See also References Navigation menue"At Least 10 Are Killed by Bombs in Iran""Iran"Archived"Arab-Iranians in Iran to make April 15 'Day of Fury'"State of Mind, State of Order: Reactions to Ethnic Unrest in the Islamic Republic of Iran.10.1111/j.1754-9469.2008.00028.x"Iran hangs Arab separatists"Iran Overview from ArchivedConstitution of the Islamic Republic of Iran"Tehran puzzled by forged 'riots' letter""Iran and its minorities: Down in the second class""Iran: Handling Of Ahvaz Unrest Could End With Televised Confessions""Bombings Rock Iran Ahead of Election""Five die in Iran ethnic clashes""Iran: Need for restraint as anniversary of unrest in Khuzestan approaches"Archived"Iranian Sunni protesters killed in clashes with security forces"Archived