12 ms·
It's far easier to use Dictionary in C# than HashMap in Rust. However we aren't comparing languages here but standard libraries. If we introduce modules in C
by DeathArrow 7y ago
It's far easier to use Dictionary in C# than HashMap in Rust.
However we aren't comparing languages here but standard libraries.
If we introduce modules in C instead of header files and add some simple data structures and algorithms to stdlib, I can see C being simpler.
C++ has data structures in its standard library and will have modules starting with C++20.
- drran 7y agoRust HashMap[0] is much simpler to use than C# Dictionary[1]: Rust: let mut map = HashMap::new(); map.insert("foo", "bar"); println!("Value: {}", map.get("baz").unwrap_or("<none>")); C# Dictionary<string, string> dict = new Dictionary<string, string>(); dict.Add("foo", "bar"); try { Console.WriteLine("Value: {0}", dict["baz"]); } catch (KeyNotFoundException) { Console.WriteLine("Value: <none>."); } [0]: https://doc.rust-lang.org/rust-by-example/std/hash.html https://doc.rust-lang.org/rust-by-example/std/hash.html [1]: https://docs.microsoft.com/en-us/dotnet/api/system.collections.generic.dictionary-2?view=netframework-4.8 https://docs.microsoft.com/en-us/dotnet/api/system.collectio...
- pjmlp 7y agoI fail to see what Rust wins in the given example, when the C# code is written properly: var dict = new Dictionary<string, string> { ["foo"] = "bar" }; Console.WriteLine($"Value: {dict.GetValueOrDefault("baz", " <none>.")}"); Better look for another example.
- nicoburns 7y agoI'm with you on this one. The Rust and C# API's are more or less equivalent. That does still repudiate the grandparent's assertion that it's easier the use C#'s dictionary.
- deleted 7y ago[deleted]
- calaphos 7y agoBut arguably what makes rust hard to write is the borrow checker which eliminates a lot of painful runtime debugging.
- ModernMech 7y agoI agree with this. I've found that students who struggle with the borrow checker also write code that tends to segfault in C. Rust is nice in that once you get it to compile it pretty much works. It's not going to segfault. I've also found that when students master the borrow checker in Rust, they write better C code.