如何在Rust中返回一个泛型结构。

huangapple go评论91阅读模式
英文:

How to return a generic struct in Rust

问题

  1. pub fn tryparse() -> (CLIAction, impl std::any::Any) {
  2. let args = CLI::from_args();
  3. match args.cmd {
  4. SubCommand::Add(opt) => {
  5. (CLIAction::QuantityAdd, Box::new(opt) as Box<dyn std::any::Any>)
  6. }
  7. SubCommand::Del(opt) => {
  8. (CLIAction::QuantityDel, Box::new(opt) as Box<dyn std::any::Any>)
  9. }
  10. SubCommand::Search(opt) => {
  11. (CLIAction::Search, Box::new(opt) as Box<dyn std::any::Any>)
  12. }
  13. }
  14. }
英文:

Beginner in Rust, I'm wondering how to return generic data in a function.

  1. pub fn tryparse() -&gt; (CLIAction, ArgSingle) {
  2. let args = CLI::from_args();
  3. match args.cmd {
  4. SubCommand::Add(opt) =&gt; {
  5. (CLIAction::QuantityAdd,opt)
  6. }
  7. SubCommand::Del(opt) =&gt; {
  8. (CLIAction::QuantityDel,opt)
  9. }
  10. }
  11. }

In this example both Add and Del get a ArgSingle struct type. However I'd like to implement a Search that would use a ArgSearch struct type, and return it... How to tell rust that tryparse() can return either ArgSingle or ArgSearch type?

答案1

得分: 1

看起来你想要从tryparse()函数中返回ArgSingleArgSearch。有几种方法可以做到这一点,其中一种常见的方法是使用枚举作为返回类型。

首先,定义一个枚举,表示函数可能返回的所有类型:

  1. enum CLIResult {
  2. Single(ArgSingle),
  3. Search(ArgSearch),
  4. }

然后,修改函数以在元组内返回这个枚举:

  1. pub fn tryparse() -> (CLIAction, CLIResult) {
  2. let args = CLI::from_args();
  3. match args.cmd {
  4. SubCommand::Add(opt) => {
  5. (CLIAction::QuantityAdd, CLIResult::Single(opt))
  6. }
  7. SubCommand::Del(opt) => {
  8. (CLIAction::QuantityDel, CLIResult::Single(opt))
  9. }
  10. SubCommand::Search(opt) => {
  11. (CLIAction::Search, CLIResult::Search(opt))
  12. }
  13. }
  14. }

现在,这个函数可以返回ArgSingle作为CLIResult::SingleArgSearch作为CLIResult::Search

要使用tryparse()的返回值,你需要对CLIResult枚举进行模式匹配,以提取适当类型的结果。例如:

  1. let (action, result) = tryparse();
  2. match result {
  3. CLIResult::Single(single_arg) => {
  4. // 处理单一参数的情况
  5. }
  6. CLIResult::Search(search_arg) => {
  7. // 处理搜索参数的情况
  8. }
  9. }
英文:

Sounds like you want to return either ArgSingle or ArgSearch from tryparse() function. There are a few approaches to that, a common one is to use enumerations as the return.

First, define an enum that represents all the possible types that can be returned from the function:

  1. enum CLIResult {
  2. Single(ArgSingle),
  3. Search(ArgSearch),
  4. }

Then, modify the function to return this enum inside of the tuple:

  1. pub fn tryparse() -&gt; (CLIAction, CLIResult) {
  2. let args = CLI::from_args();
  3. match args.cmd {
  4. SubCommand::Add(opt) =&gt; {
  5. (CLIAction::QuantityAdd, CLIResult::Single(opt))
  6. }
  7. SubCommand::Del(opt) =&gt; {
  8. (CLIAction::QuantityDel, CLIResult::Single(opt))
  9. }
  10. SubCommand::Search(opt) =&gt; {
  11. (CLIAction::Search, CLIResult::Search(opt))
  12. }
  13. }
  14. }

Now, the function can return either ArgSingle as CLIResult::Single or ArgSearch as CLIResult::Search.

To use the return value of tryparse(), you would need to pattern match on the CLIResult enum to extract the appropriate type of result. For example:

  1. let (action, result) = tryparse();
  2. match result {
  3. CLIResult::Single(single_arg) =&gt; {
  4. // Handle single argument case
  5. }
  6. CLIResult::Search(search_arg) =&gt; {
  7. // Handle search argument case
  8. }
  9. }

huangapple
  • 本文由 发表于 2023年2月27日 01:12:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/75573684.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定