Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

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.



Rust 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

[1]: https://docs.microsoft.com/en-us/dotnet/api/system.collectio...


I 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.


I'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.


But arguably what makes rust hard to write is the borrow checker which eliminates a lot of painful runtime debugging.


I 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.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: