These are chat archives for rust-lang/rust

20th
Jun 2018
Christopher Sabater Cordero
@cs-cordero
Jun 20 2018 01:34
sorry if this is kind of a noob question, i'm still very new to rust.
if i'm trying to make a vector from an array variable, how would i do that?
i.e.
let arr = [0, 1, 2, 3];
let v = vec!    // what goes here?
i know that i can do let v = vec![0,1,2,3] but how would i use the arr variable?
Christopher Sabater Cordero
@cs-cordero
Jun 20 2018 01:39
do i have to make a mutable vector first and then push each value into it?
Zack Pierce
@ZackPierce
Jun 20 2018 03:57
fn main() {
    let arr = [0, 1, 2, 3];
    let v = arr.to_vec();
    println!("{:?}", v);
}
Joseph Murphy
@JMurph2015
Jun 20 2018 07:00
So, I'm writing some FFI code and at some point I'd like to make a slice out of a FFI array and its length. Now I can't make struct fields self referential, so I can't store the slice on the struct. What's the best way to mitigate safety here? I don't like having an unsafe block on a commonly used API function (my current best idea)
Christopher Sabater Cordero
@cs-cordero
Jun 20 2018 10:57
Oooh, it was in the slice docs, not the array nor the vector docs
Thanks @ZackPierce !
Zakarum
@omni-viral
Jun 20 2018 11:48
Because it is an array
You can make a slice from it.
Fredrik Portström
@portstrom
Jun 20 2018 11:48
It's a reference to an array (a thin pointer).
Zakarum
@omni-viral
Jun 20 2018 11:48
&foo[..] is a slice
Also &foo can be coerced to slice if you try to assign it where slice is expected
Brad Gibson
@U007D
Jun 20 2018 11:49
ah, coercion--thx
Zakarum
@omni-viral
Jun 20 2018 11:49
That's why bar is slice
You declare it as such and you assign &[] (reference to array) to it which is immediately coerced to slice
Kelly Thomas Kline
@kellytk
Jun 20 2018 17:35
I'm attempting to cross-compile an actix web project on macOS for FreeBSD and I'm encountering an error I'd appreciate help with https://ghostbin.com/paste/mdf75
Robert
@rw
Jun 20 2018 19:25
is there a way to 'label' scalars without wrapping them in a struct? I want to keep type labels associated with integers to prevent programming errors. for example, a type Offset<T> = u32<T> so that I could write type MyLabel = (); let x: Offset<MyLabel> = 5u32;but the only way I see to do that is to create a struct wrapping the scalar type, and use PhantomData to hold on to the specialized type.
Fredrik Portström
@portstrom
Jun 20 2018 20:33
How do I tell Serde I want my Vec<u8> to be serialized as a byte string, not as an array of numbers? I'm currently trying out serde_cbor.
Denis Lisov
@tanriol
Jun 20 2018 20:37
Fredrik Portström
@portstrom
Jun 20 2018 20:40
Oh, cool. Thanks @tanriol
It's just a bit inconvenient #[serde(with = "serde_bytes")] isn't implemented for Vec<Vec<u8>>.
Kelly Thomas Kline
@kellytk
Jun 20 2018 20:44
Is underscore the preferred separator in project names because hyphen requires referencing as underscores in code?
Denis Lisov
@tanriol
Jun 20 2018 20:46
Not sure whether there's a "preferred" one...
Fredrik Portström
@portstrom
Jun 20 2018 20:49
There doesn't seem to be a generally preferred one, but for me it's preferred everywhere. I think people started writing names with hyphens because there was some SEO advice that circulated 10 years ago or so when SEO was cool that said that search engines will split your URLs at hyphens but not underscores, thereby giving you a better ranking for your hyphen-separated words. Search engines have probably fixed their algorithms by now so underscores work just as well as hyphens, but people keep copying popular habits without remembering the bad reasons they became popular.
Kelly Thomas Kline
@kellytk
Jun 20 2018 20:55
I personally prefer hyphens for readability but for consistency I'm leaning toward underscores in Rust project names so the same project name can be referenced in code
Zakarum
@omni-viral
Jun 20 2018 21:40
@rw tuple-types are the way to go
struct Offset<T>(u32, PhantomData<T>);
Or just struct Offset<T>(u32, T); if your labels are zero-sized anyway
type X = Y doesn't create new type. Just X and Y become synonims. So you still can do same programming errors.
Robert
@rw
Jun 20 2018 21:50
@omni-viral in that case i need to implement all the integer methods on the type, right? (or derive, which i've found doesn't seem to work like i expect)
@omni-viral i'm going for ergonomics, so let foo: Offset<A> = 32; let bar: Offset<A> = 64; let c = foo + bar; is much better than let foo: Offset<A> = Offset::new(32); ... ; let c = foo.0 + bar.0
Zakarum
@omni-viral
Jun 20 2018 21:52
Yeap. There are some talks about deriving any trait that implemented for the only field type
But right now you can only make that implmentation in a macro and reuse it
Or find it on crates.io
Christopher Sabater Cordero
@cs-cordero
Jun 20 2018 23:09
what's an "integral variable"
// causing errors on purpose to see what types the compiler thinks these vars are
let x = [1, 2, 3];
for i in x.iter() {
    let a: usize = i;  // &{integer}
    let b: usize = &i;  // &&{integer}
    let c: usize = *i;  // somehow works? ?
    let d: str = *i;  // "expected str, found integral variable"
}