rust


Traits with associated type in templates


I have been having problems compiling my Rust code, I managed to boil down the issue to this snippet:
use std::slice::Iter;
pub trait Foo<'a> {
type Bar: Iterator<Item=&'a usize>;
fn make(&self) -> usize;
}
pub struct Juice;
impl <'a> Foo<'a> for Juice {
type Bar = Iter<'a, usize>;
fn make(&self) -> usize { 0us }
}
// Uncomment this line to break things
// fn get_int<'a, T: Foo<'a>>(t: T) -> usize {
// t.make()
// }
fn main() {
println!("Hello, {:?} world!" , Juice.make());
}
I am pretty sure I am just missing something, is there anything I need to do to make this trait work? I am using the latest nightly alpha build (at the time of writing):
rustc 1.0.0-nightly (458a6a2f6 2015-01-25 21:20:37 +0000)
Unfortunately, you need to write this:
fn get_int<'a, T: Foo<'a, Bar=I>, I: Iterator<Item=&'a usize>>(t: T) -> usize {
t.make()
}
That is, you have to specify explicitly that the type of Bar is an iterator of the corresponding type. The trait bound inside the trait definition alone is insufficient.
This is very much like regular type parameters work. Even if you write something like
trait Parameterized<T: Clone> { ... }
You still need to write
fn do_something<P: Parameterized<T>, T: Clone>() { ... }
Or with structs:
struct S<T: Iterator<i32>> { ... }
impl<T: Iterator<i32>> for S<T> { ... }
This does look counterintuitive (and I've stumbled upon this several times as well) and probably deserves an issue in RFC repo.

Related Links

error with % operator inside closure
How can I open a file with the standard text editor?
How to use multiple variables in routes with Nickel?
sdl2-sys won't compile - could not exec the linker: No such file or directory
Cannot borrow as mutable more than once at a time in one code - but can in another very similar
Forcing a move for an implemented `Copy` type
When do I need to specify explicit lifetimes in Rust?
cannot borrow `self.x` as immutable because `*self` is also borrowed as mutable
Getting sequence of bytes (u8) from a char
How does the lifetime work on constant strings?
Conditionally compile only one module at a time
“macro undefined” error in the macro's own doc test
How to get mutable struct from HashMap?
Immutable reference after mutable borrow
How do I compile with “ffast-math”?
What is the difference between Copy and Clone?

Categories

HOME
events
seedstack
schema.org
semantic-web
google-webmaster-tools
mosquitto
visualforce
firebase-database
save
telegraf
elastic-load-balancer
yahoo
sslhandshakeexception
complexity-theory
texas-instruments
asp.net-core-1.0
google-cloud-logging
pst
stackexchange.redis
sipp
backendless
firemonkey-style
fusetools
tracing
beego
offline-caching
network-analysis
amazon-iam
rspec-rails
osx-lion
autoencoder
amazon-kms
cargo
forms-authentication
cheat-engine
swfupload
uipickerview
ejml
latexml
react-intl
axis
viewmodel
sony-future-lab-n
password-hash
extjs4.1
scala-collections
maatwebsite-excel
email-injection
oracle-fusion-apps
gapi
ogg
elastic4s
thread-exceptions
zynq
msbuild-4.0
jenkins-workflow
between
android-4.2-jelly-bean
jtag
dojo-build
fiber
meld
yii2-model
powerpoint-2013
jemdoc
taco
servicestack-bsd
cryptojs
bayesglm
android-mapview
place
astropy
supersocket.net
fantom
sequence-sql
mgwt
python-c-extension
dos2unix
hp-idol-ondemand
visual-studio-6
relationships
webautomation
nachos
screwturn
rails-api
fileshare
zend-search-lucene
spring-social-linkedin
meteorite
xsd2code
dynamic-rdlc-generation
session-0-isolation
jsplitpane
user-forums
cgrectmake
towers-of-hanoi
getproperty
jdom
firebird1.5
yuidoc
3des
git-log
downgrade
aptitude
ios-4.2
resharper-5.1
activestate
icicles
3270
efs
comment-conventions
rendering-engine
html-generation

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App