Hi all,
I'm playing around with Rust a little having been interested in it for a
while. I've read the language and borrowed pointers tutorials but I'm still
struggling with what seems like a fairly trivial example.
I have a StringReader type:
struct StringReader {
data: &str,
mut next: uint
}
and I'm trying to create a constructor for it:
impl StringReader {
static fn new(value: &str) -> StringReader {
StringReader { data: value, next: 0 }
}
}
I can't do this because the compiler "cannot infer an appropriate
lifetime", which I take to mean that it can't work out the lifetime of
'value', probably because I shouldn't be using a borrowed pointer here.
I tried a named lifetime, to indicate that the returned StringReader
pointer should be valid as long as the string is:
static fn new(value: &lifetime/str) -> &lifetime/StringReader {
&StringReader { data: value, next: 0 }
}
but that doesn't work either since the lifetime of the return value is not
the same as the lifetime of the param.
I also tried by value:
struct StringReader {
data: str,
mut next: uint
}
static fn new(value: str) -> StringReader {
StringReader { data: value, next: 0 }
}
but to my amazement "bare `str` is not a type" - I'd expected this just to
create an inefficient copying version.
The only thing I've managed to get to work is an owned string:
struct StringReader {
data: ~str,
mut next: uint
}
static fn new(value: ~str) -> StringReader {
StringReader { data: value, next: 0 }
}
Is this my best option here? What is the best practice for constructors?
Another thing that I don't quite understand: according to the tutorials, a
struct constructed so:
StringReader { data: value, next: 0 }
is allocated on the stack. How does this work with the return value of the
constructor? Since the return value is only in scope in the constructor
body, I suppose this means that the return value of the constructor is
copied - isn't this inefficient for constructors?
Thanks for any and all clarification,
Colin
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev