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

Thal And Out Agency railway station See also References External links Navigation menuOfficial Web Site of Pakistan RailwaysArchivedOfficial Web Site of Pakistan Railwayseeexpanding ite

Posting a File and Associated Data to a RESTful WebService preferably as JSON2019 Community Moderator ElectionREST URI convention - Singular or plural name of resource while creating itUploading both data and files in one form using Ajax?How do I upload a file with metadata using a REST web service?REST API - file (ie images) processing - best practicesSpring: JSON data and file in the same requestREST API Put large dataPosting a file and JSON data to Spring rest serviceRESTful Web Service Upload/Download Large Data With JSONREST API Design sending JSON data and a file to the api in same requestPost JSONArray to REST servicePUT vs. POST in RESTParsing values from a JSON file?JavaScript/jQuery to download file via POST with JSON dataHow do I upload a file with metadata using a REST web service?How to POST JSON data with Curl from Terminal/Commandline to Test Spring REST?Separate REST JSON API server and client?what's the correct way to send a file from REST web service to client?How do I write JSON data to a file?What is “406-Not Acceptable Response” in HTTP?REST API - file (ie images) processing - best practices

How can I change the color of pagination dots of UIPageControl?How to change UIPageControl dotsIs there a way to change page indicator dots colorCustomize dot with image of UIPageControl at index 0 of UIPageControlNo visible @interface for 'NSObject<PageControlDelegate>' declares the selector 'pageControlPageDidChange:'How to change the color of pagination dots in UIPageControl with a different color per pagepagecontrol indicator custom image instead of DefaultChanging the colour of UIPageControl dots in MonoTouchpagecontrol selectable page visibility color?Alternative way to load ViewControllers on a UIPageControlHow to set only layer.border-color for UIpage control dots in swiftHow can I develop for iPhone using a Windows development machine?How to change the name of an iOS app?UITableView - change section header coloruipagecontrol indicator(dot)issueCustom UIPageControl dots color not changingchange the interspace between UIPageControl dotsHow to change Status Bar text color in iOSHow can I change image tintColor in iOS and WatchKitUIPageControl dots with larger space in between each dotsHow to change the color of pagination dots in UIPageControl with a different color per page