Hi, list, I've read the tutorial and had a glance at the reference and
other documents.
I'm now trying to implement the GLUT library wrapper for rust, but I
got a problem.
Essentially, what I want to do is to convert main()'s args: [str] to [*c_char].
I've read the document, and I found str::as_c_str() function, and the
signature is:
fn as_c_str<T>(s: str, f: fn(*libc::c_char) -> T) -> T.
I'm wondering if it is ok to use this function as:
let my_cstr = as_c_str(my_str) {|x| x};
If it is OK, then it's easy to convert [str] to [*c_char].
But I'm afraid that the *c_char may not available at the outside of the closure.
Or, it may be possible that suddenly the *c_char became unavailable at outside,
like C++'s vector::iterator become unavailable when the original
vector has edited.
Currently, I assume it is NG, and I'm writing the convert function
using recursion like this:
fn rec(strs: [str], i: uint, cstrs: [mut *c_char]) unsafe {
if strs.len() == i {
// converted, do something with cstrs.
} else {
str::as_c_str(strs[i]) {|x| rec(strs, i+1u, cstrs + [x]) }
}
}
rec(strs, 0u, [mut]);
Of course, this code will cause stack overflow when the list is very big.
Anyone, do you know it is OK to use *c_char at outside of as_c_str()?
Thanks,
_______________________________________________
Rust-dev mailing list
[email protected]
https://mail.mozilla.org/listinfo/rust-dev