How to use instanced struct values in a match statement?












-1















I have an example instanced struct, named args, of the original struct Args.



struct Args {
arg1: bool,
arg2: bool,
}


and the instanced struct being:



let args = Args {
arg1: true,
arg2: false,
}


Using these arguments, I am attempting to avoid a jumble of if-else statments and just use a match statement. However, when attempting to perform the following:



match true {
args.arg1 => println!("Argument 1 is true!"),
args.arg2 => println!("Argument 2 is true!"),
}


I am given the error



error: expected one of `=>`, `@`, `if`, or `|`, found `.`
--> src/main.rs:13:13
|
13 | args.arg1 => println!("Argument one is true"),
| ^ expected one of `=>`, `@`, `if`, or `|` here

error: aborting due to previous error


Is there an escape character sequence I should use to avoid this, or is this simply incorrect syntax?










share|improve this question























  • Does this help ?

    – Stargateur
    Nov 23 '18 at 0:15











  • It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

    – aethio
    Nov 23 '18 at 0:20






  • 1





    I can't help you about something I can't see.

    – Stargateur
    Nov 23 '18 at 0:24











  • For example, instead of just arg1 and arg2, it has arg1 through arg10.

    – aethio
    Nov 23 '18 at 0:26
















-1















I have an example instanced struct, named args, of the original struct Args.



struct Args {
arg1: bool,
arg2: bool,
}


and the instanced struct being:



let args = Args {
arg1: true,
arg2: false,
}


Using these arguments, I am attempting to avoid a jumble of if-else statments and just use a match statement. However, when attempting to perform the following:



match true {
args.arg1 => println!("Argument 1 is true!"),
args.arg2 => println!("Argument 2 is true!"),
}


I am given the error



error: expected one of `=>`, `@`, `if`, or `|`, found `.`
--> src/main.rs:13:13
|
13 | args.arg1 => println!("Argument one is true"),
| ^ expected one of `=>`, `@`, `if`, or `|` here

error: aborting due to previous error


Is there an escape character sequence I should use to avoid this, or is this simply incorrect syntax?










share|improve this question























  • Does this help ?

    – Stargateur
    Nov 23 '18 at 0:15











  • It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

    – aethio
    Nov 23 '18 at 0:20






  • 1





    I can't help you about something I can't see.

    – Stargateur
    Nov 23 '18 at 0:24











  • For example, instead of just arg1 and arg2, it has arg1 through arg10.

    – aethio
    Nov 23 '18 at 0:26














-1












-1








-1


1






I have an example instanced struct, named args, of the original struct Args.



struct Args {
arg1: bool,
arg2: bool,
}


and the instanced struct being:



let args = Args {
arg1: true,
arg2: false,
}


Using these arguments, I am attempting to avoid a jumble of if-else statments and just use a match statement. However, when attempting to perform the following:



match true {
args.arg1 => println!("Argument 1 is true!"),
args.arg2 => println!("Argument 2 is true!"),
}


I am given the error



error: expected one of `=>`, `@`, `if`, or `|`, found `.`
--> src/main.rs:13:13
|
13 | args.arg1 => println!("Argument one is true"),
| ^ expected one of `=>`, `@`, `if`, or `|` here

error: aborting due to previous error


Is there an escape character sequence I should use to avoid this, or is this simply incorrect syntax?










share|improve this question














I have an example instanced struct, named args, of the original struct Args.



struct Args {
arg1: bool,
arg2: bool,
}


and the instanced struct being:



let args = Args {
arg1: true,
arg2: false,
}


Using these arguments, I am attempting to avoid a jumble of if-else statments and just use a match statement. However, when attempting to perform the following:



match true {
args.arg1 => println!("Argument 1 is true!"),
args.arg2 => println!("Argument 2 is true!"),
}


I am given the error



error: expected one of `=>`, `@`, `if`, or `|`, found `.`
--> src/main.rs:13:13
|
13 | args.arg1 => println!("Argument one is true"),
| ^ expected one of `=>`, `@`, `if`, or `|` here

error: aborting due to previous error


Is there an escape character sequence I should use to avoid this, or is this simply incorrect syntax?







syntax rust






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 0:05









aethioaethio

812




812













  • Does this help ?

    – Stargateur
    Nov 23 '18 at 0:15











  • It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

    – aethio
    Nov 23 '18 at 0:20






  • 1





    I can't help you about something I can't see.

    – Stargateur
    Nov 23 '18 at 0:24











  • For example, instead of just arg1 and arg2, it has arg1 through arg10.

    – aethio
    Nov 23 '18 at 0:26



















  • Does this help ?

    – Stargateur
    Nov 23 '18 at 0:15











  • It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

    – aethio
    Nov 23 '18 at 0:20






  • 1





    I can't help you about something I can't see.

    – Stargateur
    Nov 23 '18 at 0:24











  • For example, instead of just arg1 and arg2, it has arg1 through arg10.

    – aethio
    Nov 23 '18 at 0:26

















Does this help ?

– Stargateur
Nov 23 '18 at 0:15





Does this help ?

– Stargateur
Nov 23 '18 at 0:15













It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

– aethio
Nov 23 '18 at 0:20





It's good to know this is possible, but my production program is using several arguments, not two alone... Iterating over all possible combinations would be a pain :/

– aethio
Nov 23 '18 at 0:20




1




1





I can't help you about something I can't see.

– Stargateur
Nov 23 '18 at 0:24





I can't help you about something I can't see.

– Stargateur
Nov 23 '18 at 0:24













For example, instead of just arg1 and arg2, it has arg1 through arg10.

– aethio
Nov 23 '18 at 0:26





For example, instead of just arg1 and arg2, it has arg1 through arg10.

– aethio
Nov 23 '18 at 0:26












1 Answer
1






active

oldest

votes


















3














It is unclear to me what you mean to happen for the case where you have both arg1 and arg2 set to true - do you just want the first branch of the if to be taken, or both?



If its both - then really you should just use multiple ifs.



if args.arg1 { println!("Argument 1 is true!") }
if args.arg2 { println!("Argument 2 is true!") }


If only one should occur preferring arg1, then you can use a struct destructure



match args {
Args { arg1:true, ..} => println!("Argument 1 is true!"),
Args { arg2:true, ..} => println!("Argument 2 is true!"),
_ => println!("Neither is true")
}


You can use the match to match more complex cases too



match args {
Args {arg1:true, arg2:false, ...} => println!("TF"),
}


However, if at most one argument can be true at once, you really have an enum and should probably handle it like



enum Arg {
None,
Arg1,
Arg2
}

fn main() {
let args:Args = get_argument();
match args {
Args::None => println!("none"),
Args::Arg1 => println!("Argument 1 is true!"),
Args::Arg2 => println!("Argument 2 is true!")
}
}





share|improve this answer


























  • It's a good point you raise! I created this with the expectation that only one arg would be true at once.

    – aethio
    Nov 23 '18 at 0:43











  • If only one is true at once, you really have an enum. I'll update the code to detail that too.

    – Michael Anderson
    Nov 23 '18 at 0:46











  • I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

    – aethio
    Nov 23 '18 at 0:56











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%2f53439273%2fhow-to-use-instanced-struct-values-in-a-match-statement%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









3














It is unclear to me what you mean to happen for the case where you have both arg1 and arg2 set to true - do you just want the first branch of the if to be taken, or both?



If its both - then really you should just use multiple ifs.



if args.arg1 { println!("Argument 1 is true!") }
if args.arg2 { println!("Argument 2 is true!") }


If only one should occur preferring arg1, then you can use a struct destructure



match args {
Args { arg1:true, ..} => println!("Argument 1 is true!"),
Args { arg2:true, ..} => println!("Argument 2 is true!"),
_ => println!("Neither is true")
}


You can use the match to match more complex cases too



match args {
Args {arg1:true, arg2:false, ...} => println!("TF"),
}


However, if at most one argument can be true at once, you really have an enum and should probably handle it like



enum Arg {
None,
Arg1,
Arg2
}

fn main() {
let args:Args = get_argument();
match args {
Args::None => println!("none"),
Args::Arg1 => println!("Argument 1 is true!"),
Args::Arg2 => println!("Argument 2 is true!")
}
}





share|improve this answer


























  • It's a good point you raise! I created this with the expectation that only one arg would be true at once.

    – aethio
    Nov 23 '18 at 0:43











  • If only one is true at once, you really have an enum. I'll update the code to detail that too.

    – Michael Anderson
    Nov 23 '18 at 0:46











  • I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

    – aethio
    Nov 23 '18 at 0:56
















3














It is unclear to me what you mean to happen for the case where you have both arg1 and arg2 set to true - do you just want the first branch of the if to be taken, or both?



If its both - then really you should just use multiple ifs.



if args.arg1 { println!("Argument 1 is true!") }
if args.arg2 { println!("Argument 2 is true!") }


If only one should occur preferring arg1, then you can use a struct destructure



match args {
Args { arg1:true, ..} => println!("Argument 1 is true!"),
Args { arg2:true, ..} => println!("Argument 2 is true!"),
_ => println!("Neither is true")
}


You can use the match to match more complex cases too



match args {
Args {arg1:true, arg2:false, ...} => println!("TF"),
}


However, if at most one argument can be true at once, you really have an enum and should probably handle it like



enum Arg {
None,
Arg1,
Arg2
}

fn main() {
let args:Args = get_argument();
match args {
Args::None => println!("none"),
Args::Arg1 => println!("Argument 1 is true!"),
Args::Arg2 => println!("Argument 2 is true!")
}
}





share|improve this answer


























  • It's a good point you raise! I created this with the expectation that only one arg would be true at once.

    – aethio
    Nov 23 '18 at 0:43











  • If only one is true at once, you really have an enum. I'll update the code to detail that too.

    – Michael Anderson
    Nov 23 '18 at 0:46











  • I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

    – aethio
    Nov 23 '18 at 0:56














3












3








3







It is unclear to me what you mean to happen for the case where you have both arg1 and arg2 set to true - do you just want the first branch of the if to be taken, or both?



If its both - then really you should just use multiple ifs.



if args.arg1 { println!("Argument 1 is true!") }
if args.arg2 { println!("Argument 2 is true!") }


If only one should occur preferring arg1, then you can use a struct destructure



match args {
Args { arg1:true, ..} => println!("Argument 1 is true!"),
Args { arg2:true, ..} => println!("Argument 2 is true!"),
_ => println!("Neither is true")
}


You can use the match to match more complex cases too



match args {
Args {arg1:true, arg2:false, ...} => println!("TF"),
}


However, if at most one argument can be true at once, you really have an enum and should probably handle it like



enum Arg {
None,
Arg1,
Arg2
}

fn main() {
let args:Args = get_argument();
match args {
Args::None => println!("none"),
Args::Arg1 => println!("Argument 1 is true!"),
Args::Arg2 => println!("Argument 2 is true!")
}
}





share|improve this answer















It is unclear to me what you mean to happen for the case where you have both arg1 and arg2 set to true - do you just want the first branch of the if to be taken, or both?



If its both - then really you should just use multiple ifs.



if args.arg1 { println!("Argument 1 is true!") }
if args.arg2 { println!("Argument 2 is true!") }


If only one should occur preferring arg1, then you can use a struct destructure



match args {
Args { arg1:true, ..} => println!("Argument 1 is true!"),
Args { arg2:true, ..} => println!("Argument 2 is true!"),
_ => println!("Neither is true")
}


You can use the match to match more complex cases too



match args {
Args {arg1:true, arg2:false, ...} => println!("TF"),
}


However, if at most one argument can be true at once, you really have an enum and should probably handle it like



enum Arg {
None,
Arg1,
Arg2
}

fn main() {
let args:Args = get_argument();
match args {
Args::None => println!("none"),
Args::Arg1 => println!("Argument 1 is true!"),
Args::Arg2 => println!("Argument 2 is true!")
}
}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 1:36









aethio

812




812










answered Nov 23 '18 at 0:29









Michael AndersonMichael Anderson

46.2k697150




46.2k697150













  • It's a good point you raise! I created this with the expectation that only one arg would be true at once.

    – aethio
    Nov 23 '18 at 0:43











  • If only one is true at once, you really have an enum. I'll update the code to detail that too.

    – Michael Anderson
    Nov 23 '18 at 0:46











  • I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

    – aethio
    Nov 23 '18 at 0:56



















  • It's a good point you raise! I created this with the expectation that only one arg would be true at once.

    – aethio
    Nov 23 '18 at 0:43











  • If only one is true at once, you really have an enum. I'll update the code to detail that too.

    – Michael Anderson
    Nov 23 '18 at 0:46











  • I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

    – aethio
    Nov 23 '18 at 0:56

















It's a good point you raise! I created this with the expectation that only one arg would be true at once.

– aethio
Nov 23 '18 at 0:43





It's a good point you raise! I created this with the expectation that only one arg would be true at once.

– aethio
Nov 23 '18 at 0:43













If only one is true at once, you really have an enum. I'll update the code to detail that too.

– Michael Anderson
Nov 23 '18 at 0:46





If only one is true at once, you really have an enum. I'll update the code to detail that too.

– Michael Anderson
Nov 23 '18 at 0:46













I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

– aethio
Nov 23 '18 at 0:56





I haven't yet gotten to the Enums chapter of The Book yet; but your code is perfect in my situation.. thank you!!

– aethio
Nov 23 '18 at 0:56




















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%2f53439273%2fhow-to-use-instanced-struct-values-in-a-match-statement%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

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?