Why do I get a mismatched types error while trying to match a tuple?

Max Yankov

Here's some self-explanatory code that I would expect to work:

type some_t = i32;

struct SomeStruct {
    pub some_tuple_vector: Vec<(some_t, some_t)>,
}

impl SomeStruct {
    fn some_method(&mut self) {
        for p in self.some_tuple_vector.iter_mut() {
            match p {
                (x, y) if x < 0 => self.some_tuple_vector.remove(p),
                (x, y) => p = (x, y - 1),
            }
        }
    }
}

fn main() {}

However, I get errors on both lines that attempt to match and destructure the tuple:

error[E0308]: mismatched types
  --> src/main.rs:12:17
   |
12 |                 (x, y) if x < 0 => self.some_tuple_vector.remove(p),
   |                 ^^^^^^ expected mutable reference, found tuple
   |
   = note: expected type `&mut (i32, i32)`
              found type `(_, _)`

I don't understand it; isn't the type &mut (i32,i32) a tuple itself?

I've realized that I can rewrite this code to be more elegant:

self.some_tuple_vector = self.some_tuple_vector
    .iter()
    .map(|(x, y)| (x - 1, y))
    .filter(|(x, y)| x > 0);

But I get the same error:

error[E0308]: mismatched types
  --> src/main.rs:12:19
   |
12 |             .map(|(x, y)| (x - 1, y))
   |                   ^^^^^^ expected &(i32, i32), found tuple
   |
   = note: expected type `&(i32, i32)`
              found type `(_, _)`
A.B.

I don't quite understand it. Isn't the type &mut (i32, i32) a tuple itself?

No, it is a mutable reference to a tuple.

You also can't remove elements from a vector while iterating over it as this could lead to dangling pointers. remove also removes an element at index. You'll have to do things differently:

type some_t = i32;

struct SomeStruct {
    pub some_tuple_vector: Vec<(some_t, some_t)>,
}

impl SomeStruct {
    fn some_method(&mut self) {
        self.some_tuple_vector.retain(|tuple| tuple.0 >= 0);
        for tuple in &mut self.some_tuple_vector {
            tuple.1 -= 1;
        }
    }
}

fn main() {}

This could also be done in a loop in one pass, but this is probably good enough.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Why do I get a mismatched types error while trying to match a tuple?

From Dev

Why do I get error while trying to build an architecture with multiple inputs in Keras?

From Dev

Why do I get an `Incorrect Padding` Error while trying to decode my base32 string?

From Dev

error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort

From Dev

Why I get error while trying to use LaTeX in plots' label

From Dev

Why do I get a NullPointerException while trying to begin transaction?

From Dev

MySQL Table order is correct, data types match, so why do I get ERROR 1005 (HY000)

From Dev

Why do I get BUILD SUCCESSFUL while I had an error?

From Dev

Why do I get this error when trying to map on a list of lists?

From Dev

Why do i get this error when trying to check if in Corona sdk?

From Dev

Why do I get Error 2042 in VBA Evaluate INDEX/MATCH?

From Java

Why do i get a laravel error while creating a new project?

From Dev

Why do I get an error with a foreach loop inside a while loop?

From Dev

Why do I get a "permission denied" error while installing a gem?

From Dev

Why do I get a column not found error while sorting a GridView

From Dev

Why do I get a CannotAcquireResourceException while trying to connect, when I extend AbstractComboPooledDataSource?

From Dev

I am trying to compare an item from a tuple and a list but get an error

From Dev

Why do I receive an error message while trying to access some of my GNOME shell extension settings?

From Dev

Error: Malformed entry 1 while trying to get ROS Kinetic. How do I fix this?

From Dev

Why do I get the empty tuple if I type / in iPython?

From Dev

Why do I get an authorization error from Docker when I'm trying to pull a public image?

From Dev

Why do I get this error

From Dev

why I get this error while starting eclipse

From Dev

Why do I get an OpenSSL error when trying to connect to Apple Push Notification Service?

From Dev

Why do I get an error trying to install my Python service in Windows?

From Dev

Why do I get a error 400 trying to insert a CSV file with Google Drive SDK?

From Dev

Why do I get an error when trying to use a method in application_controller?

From Dev

Why do I get an error when trying to give a lexer as input of CommonTokenStream in ANTLRv4

From Dev

Why do I get a "the location is not a folder" error when trying to open files using Dash or Synapse?

Related Related

  1. 1

    Why do I get a mismatched types error while trying to match a tuple?

  2. 2

    Why do I get error while trying to build an architecture with multiple inputs in Keras?

  3. 3

    Why do I get an `Incorrect Padding` Error while trying to decode my base32 string?

  4. 4

    error: mismatched types: expected 'usize' found '&usize' raised while trying to implement bubble sort

  5. 5

    Why I get error while trying to use LaTeX in plots' label

  6. 6

    Why do I get a NullPointerException while trying to begin transaction?

  7. 7

    MySQL Table order is correct, data types match, so why do I get ERROR 1005 (HY000)

  8. 8

    Why do I get BUILD SUCCESSFUL while I had an error?

  9. 9

    Why do I get this error when trying to map on a list of lists?

  10. 10

    Why do i get this error when trying to check if in Corona sdk?

  11. 11

    Why do I get Error 2042 in VBA Evaluate INDEX/MATCH?

  12. 12

    Why do i get a laravel error while creating a new project?

  13. 13

    Why do I get an error with a foreach loop inside a while loop?

  14. 14

    Why do I get a "permission denied" error while installing a gem?

  15. 15

    Why do I get a column not found error while sorting a GridView

  16. 16

    Why do I get a CannotAcquireResourceException while trying to connect, when I extend AbstractComboPooledDataSource?

  17. 17

    I am trying to compare an item from a tuple and a list but get an error

  18. 18

    Why do I receive an error message while trying to access some of my GNOME shell extension settings?

  19. 19

    Error: Malformed entry 1 while trying to get ROS Kinetic. How do I fix this?

  20. 20

    Why do I get the empty tuple if I type / in iPython?

  21. 21

    Why do I get an authorization error from Docker when I'm trying to pull a public image?

  22. 22

    Why do I get this error

  23. 23

    why I get this error while starting eclipse

  24. 24

    Why do I get an OpenSSL error when trying to connect to Apple Push Notification Service?

  25. 25

    Why do I get an error trying to install my Python service in Windows?

  26. 26

    Why do I get a error 400 trying to insert a CSV file with Google Drive SDK?

  27. 27

    Why do I get an error when trying to use a method in application_controller?

  28. 28

    Why do I get an error when trying to give a lexer as input of CommonTokenStream in ANTLRv4

  29. 29

    Why do I get a "the location is not a folder" error when trying to open files using Dash or Synapse?

HotTag

Archive