Skip to content

Instantly share code, notes, and snippets.

@debasishg
Last active January 23, 2024 05:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save debasishg/0756ef48fa841a0f1c672c6b6a10c72a to your computer and use it in GitHub Desktop.
Save debasishg/0756ef48fa841a0f1c672c6b6a10c72a to your computer and use it in GitHub Desktop.
Rust Idiom (Trait Bounds)

Trait Bounds

Trait bounds in Rust are really powerful and also offers lots of idiomatic ways to constrain your model. Bounds are for enforcing constraints even in other languages like Scala, but Rust offers them at a different level.

Here's one example from the book Rust for Rustaceans (a great book BTW).

Suppose you want to construct a HashMap<K, V, S>, whose keys are some generic type T, value is a usize, you can write bounds like T: Hash + Eq, S: BuildHasher + Default.

pub fn doit<T>(value: T)
where T: Hash + Eq {
    let mut h = HashMap::new();
    h.insert(value, 10usize);
}

But if you want to constrain your user to just use .collect and from_iter() on the HashMap, you can just write HashMap<T, usize, S>: FromIterator. With this bound, you want your user to be able to create the collection only from an iterator. Trying to use any other method like .insert() will give compilation error.

pub fn doit_again<T>(value: T)
where HashMap<T, usize>: FromIterator<(T, usize)> {
    // `insert` will not be allowed by the compiler
    // let h: HashMap<T, usize>= HashMap::new();
    // h.insert(value, 10usize);
    let iter = [(value, 10_usize)];
    let h = HashMap::from_iter(iter);
}

By constraining the bound, you make your model intent more explicit and clear.

@dxxvi
Copy link

dxxvi commented Jan 22, 2024

I don't know much about Scala which has +, -, <: ... If Rust doesn't have them, how can it cover the use cases where they are used? If you can give Scala examples where +, -, <: ... are used, and then convert them to Rust, that'll be great :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment