Linked Questions
61 questions linked to/from Should trait bounds be duplicated in struct and impl?
3 votes
1 answer
2k views
Why do I need to repeat my generic type constraint when implementing a trait? [duplicate]
While learning Rust, I stumbled upon the following scenario. Consider this example: use std::fmt::Debug; struct Foo<T> where T: Debug, { data: T, } impl<T> Drop for Foo<T>...
0 votes
0 answers
68 views
why Result<T,E> instead of Result<T,E: Error> in rust? [duplicate]
I am new to rust and still trying to grab the concept of it. In Error trait docs it mentions Error is a trait representing the basic expectations for error values, i.e., values of type E in Result<...
0 votes
0 answers
63 views
Child trait constraint on parent trait doesn't apply [duplicate]
I have a problem in which a child trait constraint on parent trait doesn't apply. Here is a simple example: trait ValueGetter { type ValueType; fn get_value() -> Self::ValueType; } trait ...
15 votes
3 answers
6k views
"parameter `'a` is never used" error when 'a is used in type parameter bound
use std::iter::Iterator; trait ListTerm<'a> { type Iter: Iterator<Item = &'a u32>; fn iter(&'a self) -> Self::Iter; } enum TermValue<'a, LT> where LT: ...
12 votes
2 answers
2k views
Specify `Fn` trait bound on struct definition without fixing one of the `Fn` parameters
I have a struct that contains a function object: struct Foo<F> { func: F, } I want to add an Fn trait bound to the struct definition. The problem is: I do care about the first parameter (it ...
6 votes
2 answers
1k views
Is it possible to combine type constraints in Rust?
I've been working in quite a number of statically-typed programming languages (C++, Haskell, ...), but am relatively new to Rust. I often end up writing code like this: struct LeafNode<K: Ord + ...
4 votes
2 answers
2k views
Generic types that depend on another generic in Rust
I'm trying to create a struct that is generic, with a bound that the generic implement a trait. The trait is itself generic. This is in Rust 1.49.0. If I do this: trait Foo<T> {} struct Baz<...
6 votes
1 answer
2k views
Access iterator inside its for loop
I am testing a custom bi-directional iterator in rust but i ran into error error[E0382]: borrow of moved value: `iter` --> src/main.rs:40:13 | 37 | let mut iter = BiIterator::from(vec![...
2 votes
2 answers
2k views
How do I call an associated function on a generic type without providing the generic type?
I have function that is part of a struct (for context reasons) which does not take the self parameter. Furthermore, the struct itself takes a generic parameter T with some trait restrictions: trait ...
6 votes
1 answer
895 views
How can I avoid PhantomData in this struct definition?
I have a trait that looks something like this: trait Handler<C> { fn handle(&self, msg: &Message, connection: &mut C); } Instances are supposed to be chained like you would ...
4 votes
2 answers
586 views
How to combine multiple traits as a "trait tuple"?
I write a lib like: struct Foo<A: AppleTrait, B: BananaTrait, C: CarrotTrait> {...} impl<A: AppleTrait, B: BananaTrait, C: CarrotTrait> Foo<A,B,C> {...} struct Bar<A: AppleTrait,...
1 vote
2 answers
1k views
Alternative to equality constraints for associated types
I'm trying to write this in Rust: trait Foo { type T: PartialEq; fn get_t(&self) -> Self::T; } struct Bar<F1: Foo, F2: Foo> { f1: F1, f2: F2, } impl<F1: Foo, F2: Foo&...
1 vote
2 answers
969 views
Returning a closure from a method of a generic struct
I'm a newbie with Rust and I bumped into some obstacles when dealing with closures, either when returning them from functions and or methods, either when I need to store them as struct fields. Let's ...
3 votes
2 answers
1k views
Why use trait bounds in struct definitions with generic type parameters?
I can define a struct type that uses a generic type parameter with a trait bound: struct MyStruct<T: Clone> { field: T, } This prevents me me from instantiating MyStruct with a generic type ...
3 votes
1 answer
2k views
Rust struct field that implements multiple traits
I'm trying to implement a struct that holds a field which implements two traits: use meilisearch_sdk::document::Document; use serde::{Serialize,Deserialize}; trait DeserializableDocument<'a>: ...