Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508937
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:46:19+00:00 2026-05-29T22:46:19+00:00

The tutorial shows some very basic examples of pattern matching, such as matching over

  • 0

The tutorial shows some very basic examples of pattern matching, such as matching over an integer to emulate a c-style switch statement. The tutorial also shows how to do basic destructuring over a tuple type, and destructuring structures.

It seems like it should be possible to pattern match over a vector but I cannot figure out the right syntax for it and I haven’t found any examples of it.

For example, in Haskell you can easily destructure a list:

foldr :: (a -> b -> b) -> b -> [a] -> b
foldr func initValue []     = initValue
foldr func initValue (x:xs) = func initValue $ foldr initValue func xs

So, looking at a rough translation, it would be nice to be able to do:

fn foldr<A, B>(func: fn(A, B) -> B,
               initValue: B,
               vals: [A]) -> B {
  alt vals {
    [] { ret initValue; }
    _  {
      let h = vec::head(vals),
          t = vec::tail(vals);
      ret foldr(func, func(initValue, h), t);
    }
  }
}

Note: I know you could use an if statement here, I am just using this as an example of pattern matching over a vector.

This currently returns:

patterns.rs:10:4: 10:5 error: constant contains unimplemented expression type
patterns.rs:10     [] { ret initValue; }
                ^
error: aborting due to previous errors

There is an example in the tutorial for destructuring structures (defined with { .. }) and tuples (defined with ( .. )), so it seems like there should be built-in support for vectors as well considering they also contain a special syntax (defined with [ .. ]).

Feel free to correct me if I am using vectors in the wrong way as well.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:46:22+00:00Added an answer on May 29, 2026 at 10:46 pm

    I wish I could give more general advice on how to best use pattern matching on vectors, but here’s how you can use them to test for empty vectors (at least I think that’s what that Haskell code is doing…):

    use std;
    import std::io::println;
    
    fn main() {
        let empty: [int] = [];
        println(vec_alt(empty));
        println(vec_alt([1,2,3]));
    }
    
    fn vec_alt<A>(vals: [A]) -> str {
        alt vals {
            x if x == [] { "empty" }
            _ { "otherwise" }
        }
    }
    

    Note that trying to simply pass [] as an argument fails because the compiler can’t infer a type for the vector. It seems to be possible to pass [()] (a vector with a nil inside) without first declaring it, but the alt statement seems incapable of testing to see if the head expression matches [()] (it simply falls through to the default).

    All in all, vectors seem a little rough at the moment. If there’s some specific use you have in mind that Rust doesn’t seem to support, the developers are quite open to suggestions and critcism: https://mail.mozilla.org/listinfo/rust-dev

    Also see the reference manual for a more formal definition, and a few more examples to help clarify things: http://doc.rust-lang.org/doc/rust.html#alternative-expressions

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm going through a very basic php tutorial and am creating a calculator so
I'd like some kind of overview tutorial that shows how this works. I have
The hobo lifecycle tutorial shows how to implement a friendship logic in the model
Did actually someone make already a tutorial that shows how to customize an UITableView?
In this tutorial I am reading , Dave Ward creates a page that shows
I'm working on keyboard input for a very basic kernel that I'm developing and
Sticking my toe into the Silverlight/Blend pool, I've worked through some very cool video
Is there a tutorial somewhere which shows how to use spring framework with maven
I have a very simple SQL query that I made which retrieves some user
I'm looking for some tutorials/resources/tips that will show me how to run applications via

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.