Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Rust have Collection traits?

I'd like to write a library that's a thin wrapper around some of the functionality in BTreeMap. I'd prefer not to tightly couple it to that particular data structure though. Strictly speaking, I only need a subset of its functionality, something along the lines of the NavigableMap interface in Java. I was hoping to find an analogous trait I could use. I seem to recall that at some point there were traits like Map and MutableMap in the standard library, but they seem to be absent now.

Is there a crate that defines these? Or will they eventually be re-added to std?

like image 607
zslayton Avatar asked Feb 02 '16 17:02

zslayton


People also ask

Does Rust have inheritance?

Inheritance as a Type System and as Code SharingIf a language must have inheritance to be an object-oriented language, then Rust is not. There is no way to define a struct that inherits the parent struct's fields and method implementations.

Does Rust have polymorphism?

In Rust, polymorphic functions are fully type-checked when they are declared, not when they are used. This means you can never call a Rust function and get a type error within the function because you gave it the wrong type.

Why does Rust have traits?

A trait in Rust is a group of methods that are defined for a particular type. Traits are an abstract definition of shared behavior amongst different types. So, in a way, traits are to Rust what interfaces are to Java or abstract classes are to C++. A trait method is able to access other methods within that trait.

Is Rust a trait interface?

Rust is not an object oriented language. And traits are not exactly interfaces. When you say, that a struct has some trait , it does not necessarily mean, that it is a property of the struct, but rather that struct maps with the functionality of the trait.


Video Answer


1 Answers

No, right now there's only Iterator. MutableMap and Map have been removed somewhere along the road to stabilization of std for Rust 1.0.

There have been various discussions about re-adding traits to std. See these discussions on Rust internals:

  • Traits that should be in std, but aren’t

or (less recent but more specifically on collections):

  • Collection Traits, Take 2

Bottom line: everybody wants some form of those traits in std but nobody wants to commit adding and supporting the wrong ones in the standard library until a clearer picture of what is ergonomic emerges.

like image 119
Paolo Falabella Avatar answered Sep 18 '22 15:09

Paolo Falabella