Thanks for all the answers.
On Thu, Feb 21, 2013 at 9:17 AM, Niko Matsakis <[email protected]> wrote:
>
>
> 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.
>
>
O wow, I don't know how I missed that. Coming from other languages, though
"join" seem to be the common name for this. Even the following search does
not bring up any reference to connect.
http://www.google.com/search?q=site%3Astatic.rust-lang.org%2Fdoc%2F+join
Perhaps a note could be made in the docs indicating that vec::connect()
acts like a vector join with a string separator in various other languages?
> 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.
>
>
C++ has both class- and namespace-level constants as they have different
uses. In the example, I have a class/struct-level constant. For example
struct Vector2<T> {
enum { N = 2 };
T m_v[N];
};
struct Vector3<T> {
enum { N = 3 };
T m_v[N];
};
Imagine also, as in my macro question below -- a vector struct is created
based on some integer dimension value. So that dimension constant should
logically be defined in the struct in that case as well.
So, I am to presume from your answer that there is no way to create
class-associated constants? Is this worth considering adding to Rust?
> 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.
>
>
I look forward to anyone else taking a stab at how one could accomplish
something similar in Rust. One obvious but not very DRY way would be to
send in all pre-concatenated identifiers. Like in this case, one could
re-write the macro to be invoked as VectorT(Vector3, 3). If the macro
creates multiple identifiers (including some associate with helper
classes/functions) based on just concatenating the numeric value, then the
invocation could get pretty ugly and would leak too much about the
underlying implementation.
Thanks,
Ashish
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev