Conversion/assign value into char array in structure

Hi,

I want to convert below C code into RUST.

struct Msg
{
	unsigned short m_hashedPasswordLength;
	unsigned short m_reserved1;
	unsigned __int64 m_requestId1;
	unsigned __int64 m_requestId2;
	char m_loginId[MAX_EMAILADDRESS_SIZE - 1];
	unsigned char m_hashedPassword[MAX_PASSWORDHASH_SIZE];
	unsigned char m_executorMask;
	unsigned char m_reserved2;
	unsigned short m_reserved3;
};

I facing issue with char array type, how can we assign value into [m_loginId, m_hashedPassword] variables when I make mutable variable of that structure.

Thanks

In Rust, arrays aren't special when it comes to assignment. In fact, no type really is – assignment works for all types (of which the size is statically known). If you have an array in a struct, you can assign to it using the_struct.the_array_field = another_array;.

One thing to keep in mind is that an array of characters is not how you usually represent strings in idiomatic Rust. You should probably use the String type instead.

Hi H2CO3,

Its a legacy code that I will have to use this structure to send in message, I define a mut variable of struct and assigning value array so facing problem.

//structure in RUST
struct TReqMobileBinLoginIdLong{

    m_length:u16,
    m_type:u16,
    m_hashedpasswordlength:u16,
	m_reserved1:u16,
	m_requestId1:u64,
	m_requestId2:u64,
	m_loginid:[u8;20],
	m_hashedpassword:[u8;20],
	m_executormask:c_uchar,
	m_reserved2:c_uchar,
	m_reserved3:c_uchar
}

//mutable variable
let mut loginMsg = TReqMobileBinLoginIdLong{

                        m_length:64,
                        m_type:63125,
                        m_hashedpasswordlength:u16,
                        m_reserved1:1,
                        m_requestId1:1,
                        m_requestId2:1,
                        m_loginid: //facing issue here
                        m_hashedpassword: // facing issue here..
                        m_executorMask:0,
                        m_reserved2:0,
                        m_reserved3:0
                    };

Thanks

Are you asking how to create an array literal? I don't understand what other "issue you might be facing".

Yes,

when trying to assign like below.

m_loginid:("test").as_bytes()

getting error

Thanks

You should read the documentation or at the very minimum the signature of the functions you are using. str::as_bytes() returns a reference to a dynamically-sized slice, and not a value of type array.

If you want a literal array of known size, you can initialize it using array literal syntax, listing each element in order:

let login_msg = TReqMobileBinLoginIdLong {
    ...
    loginid: [1, 2, 3],
    ...
};

or in the case of arrays of bytes, the special b"..." literal syntax is also available, which returns a reference to a 'static array-of-u8 of the appropriate length:

let login_msg = TReqMobileBinLoginIdLong {
    ...
    loginid: *b"foo@mail.com",
    ...
};

By the way, drop the m_ prefix from your field names. They serve no purpose in Rust.

Mind you, from the C declaration (MAX_EMAILADDRESS_SIZE) it looks like the array is expected to be initialized with at most that many characters, in which case a b"byte string literal" won't work (unless you manually pad it to the correct size, presumably with \x00 if this is expected to be consumed by a C API).

In this case, you can just initialize it with all zeroes using the repeating array literal construct, [0; MAX_EMAIL_ADDRESS_SIZE + 1], and then use <[u8]>::copy_from_slice() on the appropriate subrange in order to copy the contents of another slice, e.g.:

login_msg.login_id[..email.len()].copy_from_slice(&email);

or something along those lines.

2 Likes