0

What I am looking for is a way to have an array like this in Rust. This example is in PHP:

<?php

$array = [
  "key" => "value",
];


?>

As you can see what I'm looking for is an array that has keys and values. But now, how can I do the same thing in rust? I'm new to arrays in rust, so sorry if this is basic, but I'm not really into arrays.

DFG
  • 31
  • 1
  • 13
  • 1
    PHP is unusual in using its "array" datatype, as well as for what every other language considers an array (an ordered data collection with numeric keys), also for a data structure with string keys - which other languages call variously a dictionary, object or hashmap. In the case of Rust, it's a [HashMap](https://doc.rust-lang.org/book/ch08-03-hash-maps.html) you're after. – Robin Zigmond Oct 31 '22 at 22:01
  • Please do not use comments as a way to resolve the asked question. Instead, please search the site for pre-existing answers and flag/vote to close duplicate questions. – mickmackusa Oct 31 '22 at 22:16
  • Perhaps topical: [Array of arrays in Go or Rust?](https://stackoverflow.com/q/26196091/2943403) – mickmackusa Oct 31 '22 at 22:20
  • @mickmackusa apologies, I didn't actually know this was a duplicate (Rust is not a topic I know exhaustively on SO and it wasn't obvious to me this specific question would have been asked before) - and while I realise I could have put this in an answer, I prefer to at least comment first when the answer is simple enough to fit into one. – Robin Zigmond Oct 31 '22 at 22:26
  • I have never used Rust myself. I've never answered a Rust question. I've not studied Rust at all. BUT let's be honest, Stack Overflow has MILLIONS of questions and answers. In 2022, virtually all basic questions are duplicates. There is a high likelihood that this asker did not do exhaustive research, because when I Googled "Declare associative array in Rust", I was immediately shown the page that I used as the dupe target. – mickmackusa Oct 31 '22 at 22:28

1 Answers1

1

What you are looking for is a HashMap.

If you are new to the language consider reading The Book.

Aleksander Krauze
  • 3,115
  • 7
  • 18
  • 1
    Please vote to close duplicate questions instead of answering them so that the [Roomba](https://stackoverflow.com/help/roomba) can do its important work. – mickmackusa Oct 31 '22 at 22:16
  • @mickmackusa I've answered it before it was flagged. – Aleksander Krauze Oct 31 '22 at 22:28
  • 1
    I'm asking you to be thoughtful and proactively flag/vote to close pages on Stack Overflow. Stack Overflow needs contributors to, first, ask themselves if this question _should_ be answered BEFORE thinking about how they will write an answer. You shouldn't need to be provoked by another user. Please be mindful to not create redundant content on Stack Overflow. I know you are just trying to help, but you will be most helpful if you help to prevent redundant content on this site. – mickmackusa Oct 31 '22 at 22:30