89122

Simplify where clause with repeated associated type restrictions

I wrote the following function to sum over an iterator:

fn sum_iter<I>( s : I, init : &I::Item ) -> I::Item where I : Iterator + Clone, <I as Iterator>::Item : Add< I::Item, Output = I::Item> + Copy { s.clone().fold(*init, |acc, item| acc + item) }

This compiles fine under rustc-1.0.0-beta, but it would be nice if one could avoid repeating I::Item four times and instead refer to a type T and say somewhere that Iterator::Item = T only once. What's the right way to do this?

Answer1:

Yes, you can simply add T as a type parameter for your function, and require I to implement Iterator<Item=T>, like this:

fn sum_iter<I, T>( s : I, init : &T ) -> T where I : Iterator<Item=T> + Clone, T : Add<T, Output=T> + Copy { s.clone().fold(*init, |acc, item| acc + item) }

Recommend

  • Crossvalidation in Stanford NER
  • IE11 bug - calling javascript getAttributeNode method brakes mergeAttributes
  • Efficiency: Max difference between two items in a subset of an integer array
  • Problem occurs when using EGit for cloning repository in Eclipse (Missing unknown)
  • git receive-pack stops with 0000
  • Index of vector iterator
  • BASH - Shuffle characters in strings from file
  • Fold function in Octave
  • java.lang.NullPointerException: No FileItemFactory has been set
  • check if numbers have the same sign
  • Forcing a context switch from the userland on Linux?
  • Suspending event listeners
  • Sorting a HashMap, while keeping duplicates
  • AngularJS : transclude ng-repeat inside directive
  • Detecting # in Scheme list
  • How to open html table in xls on click of a button
  • Most efficient way to move table rows from one table to another
  • R convert summary result (statistics with all dataframe columns) into dataframe
  • Approximate Order-Preserving Huffman Code
  • std::remove_copy_if_ valgrind bytes in block are possibly lost in loss record
  • Grails calculated field in SQL
  • Functions in global context
  • Can Jackson SerializationFeature be overridden per field or class?
  • Is possible to count alias result on mysql
  • Deserializing XML into class C#
  • Function pointer “assignment from incompatible pointer type” only when using vararg ellipsis
  • Rearranging Cells in UITableView Bug & Saving Changes
  • XCode can't find symbols for a specific iOS library/framework project
  • Circular dependency while pushing http interceptor
  • Linker errors when using intrinsic function via function pointer
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Angular 2 constructor injection vs direct access
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • python draw pie shapes with colour filled
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • How to push additional view controllers onto NavigationController but keep the TabBar?