Closed
Description
use std::ops::Index;
struct S {
x: Vec<String>
}
impl Index<uint, str> for S {
fn index<'a> (&'a self, n: &uint) -> &'a str {
self.x[*n].as_slice()
}
}
fn main() {
let s = S { x: vec![ "X".to_string() ] };
println!("{}", &s[0]);
}
rustc: /build/rust-git/src/rust/src/llvm/lib/IR/Instructions.cpp:545: void llvm::InvokeInst::init(llvm::Value*, llvm::BasicBlock*, llvm::BasicBlock*, llvm::ArrayRef<llvm::Value*>, const llvm::Twine&): Assertion
((Args.size() == FTy->getNumParams()) || (FTy->isVarArg() && Args.size() > FTy->getNumParams())) && "Invoking a function with bad signature"' failed.
Aborted`
Also, &s[0]
is ugly.
Another approach:
use std::ops::Index;
struct S {
x: Vec<String>
}
impl Index<uint, & &str> for S {
fn index<'a> (&'a self, n: &uint) -> & &'a str {
&self.x[*n].as_slice()
}
}
fn main() {
let s = S { x: vec![ "X".to_string() ] };
println!("{}", s[0]);
}
This will get nowhere because self.x[*n].as_slice()
creates a local string slice object which will quickly goes out of scope. Perhaps we need to wait for introduction of the IndexGet
trait.
Metadata
Metadata
Assignees
Labels
No labels