4 ms·
Rust HashMap[0] is much simpler to use than C# Dictionary[1]: Rust: let mut map = HashMap::new(); map.insert("foo", "bar"); println!("Value: {}",
by drran 7y ago
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 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]