Hello again,
@2e71828 this is not a recommendation for improving the Rust language kind of grade post, this is so simple trivia that any beginner can relate with, that’s why I called the 3 things low hanging fruit.
And the fact that I didn’t even known that there was a forum called https://internals.rust-lang.org , thanks for telling me that, it’s nice to know to post more serious internals questions when the need arises.
1º Macros
@alice and @erelde, I think that any good programming language should be playful, should have the characteristic that makes you reach for it just for the having a nice time with it because, maybe one is bored or because one simply wants to play with something. And that was what happen to me yesterday. I had some free time and I wanted to play around with graph algorithms, to remember somethings and to learn some new things in a joyful and playful spirit. Python has this playful quality and I think Rust also has this quality. I have a really nice time playing with Rust.
I started by studying and porting to Rust some graph algorithms from the following Python algorithms repository:
And then when I was on the port of the second algorithm Articulation Points I came to the following very clean Python code to implement a simple demo graph:
# Adjacency list of graph
data = {
0: [1, 2],
1: [0, 2],
2: [0, 1, 3, 5],
3: [2, 4],
4: [3],
5: [2, 6, 8],
6: [5, 7],
7: [6, 8],
8: [5, 7],
}
And I look around for a good way to do this in Rust and have found in the Rust documentation what @alice wrote, and made this:
// Adjacency list of graph.
let data: GraphAP = HashMap::from([
(0, vec![1, 2]),
(1, vec![0, 2]),
(2, vec![0, 1, 3, 5]),
(3, vec![2, 4]),
(4, vec![3]),
(5, vec![2, 6, 8]),
(6, vec![5, 7]),
(7, vec![6, 8]),
(8, vec![5, 7]),
]);
For Rust, it was a better way to write it then just making HashMap inserts and I was happy with it, but then I looked at it and it was not has playful has Python version. I asked to myself if there couldn’t exist a macro that would make a better jobs in a more aesthetically pleasant way, because it started to resemble my LISP and Scheme days 25 years later (for myself) hehehe
So I found maplit, and loved it’s simplicity and it’s playful characteristic that the dictionaries in Python also have, so Rust was in pair with Python in this regard, and that is really nice, but then I sow that I had to add to toml a crate and had to make that long import and that simply messes all the yin yang of the playful characteristic that I was talking about, so my question arise:
Why isn’t this macro in the STD?
With the from:
// Adjacency list of graph.
let data: GraphAP = HashMap::from([
(0, vec![1, 2]),
(1, vec![0, 2]),
(2, vec![0, 1, 3, 5]),
(3, vec![2, 4]),
(4, vec![3]),
(5, vec![2, 6, 8]),
(6, vec![5, 7]),
(7, vec![6, 8]),
(8, vec![5, 7]),
]);
With the macro:
#[macro_use] extern crate maplit;
let map = hashmap!{
0 => vec![1, 2],
1 => vec![0, 2],
2 => vec![0, 1, 3, 5],
3 => vec![2, 4],
4 => vec![3],
5 => vec![2, 6, 8],
6 => vec![5, 7],
7 => vec![6, 8],
8 => vec![5, 7]
};
2º Multi-dimensional Arrays ans Vec’s.
@erelde, you explained that it isn’t very common the 2D and 3D arrays (stack) and Vec (heap), well as you also said it’s common in scientific computing(things like NumPy prove it) and in learning, and it is also a playful characteristic that I think Rust could have. Easy in, do something in your free time just for pleasure (in algorithms, in science or engineering, in mathematics or in physics) and easy out, it would remove some friction, some boiler plate. It would be very nice
Just my 2 cents!
3º Graph’s
I have to follow @alice recommendation and learn more about:
petgraph
Graph data structure library. Provides graph types and graph algorithms.
https://crates.io/crates/petgraph
But I really would like to see Graph’s to have a place in the STD because they area common, if you thing of graphs as graphs, tree’s and networks, that they are in reality the some thing under the hood.
Thank you,
Best regards and have a nice day,
João