メモ2
4392 ワード
http://doc.rust-lang.org/0.12.0/guide.html
8複合タイプtuplesタプル:秩序、固定サイズ
letは一つのtupleをばらばらにしてもいいです。
8.3 tuple struct定義:structとの違いでfieldは名前がなくて、最後のfieldの後で余分なものがなくて、号、{}は()になりました。
一つの要素しか含まれていないtuple structをnewtypeといいますが、何か特別なところがありますか?
letでnewtypeを散布できます。
8複合タイプtuplesタプル:秩序、固定サイズ
let x = (1i, "hello");
1 iはliに見えます。~~~日、またはlet x: (int, &str) = (1, "hello");
&strはa string sliceと読んでいます。tupleのタイプも一つのtupleのように見えます。ただタイプの中であげたのはタイプ名です。右の値は値です。letは一つのtupleをばらばらにしてもいいです。
let (x, y, z) = (1i, 2i, 3i);
println!("x is {}", x);
は、tupleによって多値戻りを実現する(つまり、tupleを返した)。fn next_two(x: int) -> (int, int) { (x + 1i, x + 2i) }
fn main() {
let (x, y) = next_two(5i);
println!("x, y = {}, {}", x, y);
}
構造体のstruct:javascriptの対象literalと全く同じで、tuple用の()、struct用の{}、tupleはキーワードが必要ではないと思います。structは2つの単語を多く書きます。structはfieldごとに名前があります。無秩序で、定義するstructデータのメンバーを使って修正できます。struct Point {
x: int,
y: int,
}
fn main() {
let origin = Point { x: 0i, y: 0i };
println!("The origin is at ({}, {})", origin.x, origin.y);
}
改訂版:struct Point {
x: int,
y: int,
}
fn main() {
let mut point = Point { x: 0i, y: 0i };
point.x = 5;
println!("The point is at ({}, {})", point.x, point.y);
}
Javascriptオブジェクトの字面量とは異なるところを発見したのは、最後の変数定義の後に「号」があるということです。!!これは進歩です。8.3 tuple struct定義:structとの違いでfieldは名前がなくて、最後のfieldの後で余分なものがなくて、号、{}は()になりました。
struct Color(int, int, int);
struct Point(int, int, int);
は{}を使って()になりました。fieldは名前を必要としません。let black = Color(0, 0, 0);
let origin = Point(0, 0, 0);
以上の定義はstructで実現します。struct Color {
red: int,
blue: int,
green: int,
}
struct Point {
x: int,
y: int,
z: int,
}
structで書いたコードの方が分かりやすい(位置より名前がはっきりしている)ので、tuple structを使わないでください。一つの要素しか含まれていないtuple structをnewtypeといいますが、何か特別なところがありますか?
letでnewtypeを散布できます。
struct Inches(int);
let length = Inches(10);
let Inches(integer_length) = length;
println!("length is {} inches", integer_length);
enum列挙(javaと同じ)enum Ordering {
Less,
Equal,
Greater,
}
の使い方:fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
fn main() {
let x = 5i;
let y = 10i;
let ordering = cmp(x, y);
if ordering == Less {
println!("less");
} else if ordering == Greater {
println!("greater");
} else if ordering == Equal {
println!("equal");
}
}
パラメータ付きエンumenum OptionalInt {
Value(int),
Missing,
}
fn main() {
let x = Value(5);
let y = Missing;
match x {
Value(n) => println!("x is {:d}", n),
Missing => println!("x is missing!"),
}
match y {
Value(n) => println!("y is {:d}", n),
Missing => println!("y is missing!"),
}
}
match(別れif else if)let x = 5i;
match x {
1 => println!("one"),
2 => println!("two"),
3 => println!("three"),
4 => println!("four"),
5 => println!("five"),
_ => println!("something else"),
}
の他の例:fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
fn main() {
let x = 5i;
let y = 10i;
match cmp(x, y) {
Less => println!("less"),
Greater => println!("greater"),
Equal => println!("equal"),
}
}
matchも表現です。fn cmp(a: int, b: int) -> Ordering {
if a < b { Less }
else if a > b { Greater }
else { Equal }
}
fn main() {
let x = 5i;
let y = 10i;
let result = match cmp(x, y) {
Less => "less",
Greater => "greater",
Equal => "equal",
};
println!("{}", result);
}
end