Ashish Myles wrote:
Lots of questions.
1. Is there an array string join function? I looked through possible
pertinent vector- and string-related modules and I didn't see
something equivalent.
vec::connect() will join a vector of strings into one string.
2. Any particular reason to_str() for arrays (defined in in std::to_str)
is defined only for unique arrays? i.e.
impl<A: ToStr> ~[A]: ToStr {
// ...
}
rather than for all array types via &?
impl<A: ToStr> &[A]: ToStr {
// ...
}
Modifying the definition seems to allow it to work for managed and
static
arrays.
I don't think there's a good reason for this.
3. What is the rust idiom for defining a constant value associated with a
class?
Declare a constant in the same module as the struct. Whereas C++ uses
the class/struct as the primary organizing unit for code, Rust tends to
use modules to group together related functions, types, constants, and
so forth. Similarly, the Rust translation for a C++ class is a module
containing a struct, impl, etc.
4. How do I copy between mutable/immutable types and from dynamic to
static
array types?
let a : ~[int] = ~[1,2,3];
// I know these are by design, but I don't know how to get the desired
// effect.
let b : [int * 3] = copy a; // fails due to storage mismatch
Hmm, I don't know of a convenient way to do this. Obviously it would
fail if the length of `a` were incorrect. You probably have to do
something like the following:
let mut b: [int*3] = [0, ..3]; // initialize with zeroes
assert a.len() == 3;
for uint::range(0, 3) |i| { b[i] = a[i]; }
I imagine there is a "memcpy" like function for vectors that you could
use to avoid the loop, but I don't know it's name off the top of my head.
let c : ~[mut int] = copy a; // fails due to mutability mismatch
You should not be using a type like ~[mut int]. That is deprecated.
Mutability is inherited through ~ pointers (indeed through any kind of
ownership), so you can write:
let mut c = a; // moves a by default, use `copy a` if you want to
use `a` later
c[0] += 1; // and so on...
5. Consistency of len/size:
Each language seems to have its own standard for specifying container
size. I thought Rust's convention was len from vector, dvec, list, etc,
but deque uses size. Should that also be len? Or rather, as "size" is
the more generic term (applies semantically to arbitrary containers
like
trees and maps), should "size" be the standard? Or, like ruby, perhaps
both could be allowed.
Good question. This is the sort of thing that we are going to be
addressing as we cleanup the libraries. I agree we should settle on
one. I don't personally care which it is. I'm personally not bothered
by the semantic mismatch of a set having a length, but I guess `size()`
would be the most broadly applicable and least likely to inspire later
flame wars.
6. Is there any way to get a number into a macro and splice it into a
class
name? i.e. Something like the intention behind the following.
macro_rules! VectorT {
// invoke it like VectorT<N>
($name:ident, $n:expr) => (
struct Vector$n<T> {
priv m_v: [mut T * $n];
}
);
}
I don't know the answer to this.
regards,
Niko
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev