Re: [rust-dev] struct def
On 06/11/13 18:46, spir wrote: I can write this: struct Points {xs:~[uint], ys:~[uint]} fn main () { let mut ps = Points{xs:~[1u], ys:~[1u]}; ... } But I cannot write that: struct Points {xs:~[T], ys:~[T]} fn main () { let mut ps = Points{xs:~[1u], ys:~[1u]}; ... } In the second case, I get the error: sparse_array.rs:106:31: 106:32 error: expected one of `; }` but found `:` sparse_array.rs:106let mut ps = Points{xs:~[1u], ys:~[1u]}; ^ The correct syntax is `let mut ps = Points:: { xs: ~[1u], ys: ~[1u] };`, but this actually does nothing, it's totally ignored ( https://github.com/mozilla/rust/issues/9620 ). In general, type inference means that you can normally just write `let mut ps = Points { xs: ~[1u], ys: ~[1u] }` even with the generic declaration of `Points`. Sorry to bother you with that, I find myself unable to find the right syntactic schema (and could not find any example in any doc online). I'm blocked, stupidly. spir@ospir:~$ rust -v rust 0.8 host: x86_64-unknown-linux-gnu Also, I have a general problem with writing struct instances with the type apart; meaning, without any type param, I get the same error: struct Points {xs:~[uint], ys:~[uint]} fn main () { let mut ps : Points = {xs:~[1], ys:~[1]}; ... } ==> parse_array.rs:106:28: 106:29 error: expected one of `; }` but found `:` sparse_array.rs:106let mut ps : Points = {xs:~[1], ys:~[1]}; ^ You need to write the struct name always, since { } is valid expression, e.g. `let x = { foo(); bar(); baz() };`. So, in this case, `let mut ps: Points = Points { xs: ~[1], ys: ~[1] };`. More generally, I don't know why there are 2 syntactic schemas to define vars. I would be happy with the latter alone (despite the additional pair of spaces) since it is more coherent and more general in allowing temporalily uninitialised declarations. Which two schema are you referring to? `let x: Type = value;` vs. `let x = value;`? If so, they're actually the same, the first just has the optional type specified. (One can provide temporarily uninitialised declarations by just `let x;`, as long as the compiler has enough information to (a) know that the variable is assigned before every use, and (b) infer the type.) Huon ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
Re: [rust-dev] struct def
I would think: let mut ps = *Points* {xs:~[1], ys:~[1]}; let mut ps : *Points* = *Points* {xs:~[1], ys:~[1]}; In Haskell-speak, there is a different between the "type" and the "constructor", even though by convention they are given the same name. On Wed, Nov 6, 2013 at 9:46 AM, spir wrote: > > I can write this: > struct Points {xs:~[uint], ys:~[uint]} > fn main () { >let mut ps = Points{xs:~[1u], ys:~[1u]}; >... > } > > But I cannot write that: > struct Points {xs:~[T], ys:~[T]} > fn main () { >let mut ps = Points{xs:~[1u], ys:~[1u]}; >... > } > > In the second case, I get the error: > sparse_array.rs:106:31: 106:32 error: expected one of `; }` but found `:` > sparse_array.rs:106let mut ps = Points{xs:~[1u], ys:~[1u]}; >^ > > Sorry to bother you with that, I find myself unable to find the right > syntactic schema (and could not find any example in any doc online). I'm > blocked, stupidly. > > spir@ospir:~$ rust -v > rust 0.8 > host: x86_64-unknown-linux-gnu > > > Also, I have a general problem with writing struct instances with the type > apart; meaning, without any type param, I get the same error: > struct Points {xs:~[uint], ys:~[uint]} > > fn main () { >let mut ps : Points = {xs:~[1], ys:~[1]}; >... > } > ==> > parse_array.rs:106:28: 106:29 error: expected one of `; }` but found `:` > sparse_array.rs:106let mut ps : Points = {xs:~[1], ys:~[1]}; > ^ > > More generally, I don't know why there are 2 syntactic schemas to define > vars. I would be happy with the latter alone (despite the additional pair > of spaces) since it is more coherent and more general in allowing > temporalily uninitialised declarations. > > Denis > ___ > Rust-dev mailing list > Rust-dev@mozilla.org > https://mail.mozilla.org/listinfo/rust-dev > ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev
[rust-dev] struct def
I can write this: struct Points {xs:~[uint], ys:~[uint]} fn main () { let mut ps = Points{xs:~[1u], ys:~[1u]}; ... } But I cannot write that: struct Points {xs:~[T], ys:~[T]} fn main () { let mut ps = Points{xs:~[1u], ys:~[1u]}; ... } In the second case, I get the error: sparse_array.rs:106:31: 106:32 error: expected one of `; }` but found `:` sparse_array.rs:106let mut ps = Points{xs:~[1u], ys:~[1u]}; ^ Sorry to bother you with that, I find myself unable to find the right syntactic schema (and could not find any example in any doc online). I'm blocked, stupidly. spir@ospir:~$ rust -v rust 0.8 host: x86_64-unknown-linux-gnu Also, I have a general problem with writing struct instances with the type apart; meaning, without any type param, I get the same error: struct Points {xs:~[uint], ys:~[uint]} fn main () { let mut ps : Points = {xs:~[1], ys:~[1]}; ... } ==> parse_array.rs:106:28: 106:29 error: expected one of `; }` but found `:` sparse_array.rs:106let mut ps : Points = {xs:~[1], ys:~[1]}; ^ More generally, I don't know why there are 2 syntactic schemas to define vars. I would be happy with the latter alone (despite the additional pair of spaces) since it is more coherent and more general in allowing temporalily uninitialised declarations. Denis ___ Rust-dev mailing list Rust-dev@mozilla.org https://mail.mozilla.org/listinfo/rust-dev