Skip to content

New array syntax pt2 #20387

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jan 2, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions src/doc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -1606,18 +1606,18 @@ things. The most basic is the **array**, a fixed-size list of elements of the
same type. By default, arrays are immutable.

```{rust}
let a = [1i, 2i, 3i]; // a: [int, ..3]
let mut m = [1i, 2i, 3i]; // mut m: [int, ..3]
let a = [1i, 2i, 3i]; // a: [int; 3]
let mut m = [1i, 2i, 3i]; // mut m: [int; 3]
```

There's a shorthand for initializing each element of an array to the same
value. In this example, each element of `a` will be initialized to `0i`:

```{rust}
let a = [0i, ..20]; // a: [int, ..20]
let a = [0i; 20]; // a: [int; 20]
```

Arrays have type `[T,..N]`. We'll talk about this `T` notation later, when we
Arrays have type `[T; N]`. We'll talk about this `T` notation later, when we
cover generics.

You can get the number of elements in an array `a` with `a.len()`, and use
Expand Down
8 changes: 4 additions & 4 deletions src/doc/reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -1438,11 +1438,11 @@ the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs.
const BIT1: uint = 1 << 0;
const BIT2: uint = 1 << 1;

const BITS: [uint, ..2] = [BIT1, BIT2];
const BITS: [uint; 2] = [BIT1, BIT2];
const STRING: &'static str = "bitstring";

struct BitsNStrings<'a> {
mybits: [uint, ..2],
mybits: [uint; 2],
mystring: &'a str
}

Expand Down Expand Up @@ -2923,7 +2923,7 @@ constant expression that can be evaluated at compile time, such as a
```
[1i, 2, 3, 4];
["a", "b", "c", "d"];
[0i, ..128]; // array with 128 zeros
[0i; 128]; // array with 128 zeros
[0u8, 0u8, 0u8, 0u8];
```

Expand Down Expand Up @@ -3691,7 +3691,7 @@ An example of each kind:

```{rust}
let vec: Vec<int> = vec![1, 2, 3];
let arr: [int, ..3] = [1, 2, 3];
let arr: [int; 3] = [1, 2, 3];
let s: &[int] = vec.as_slice();
```

Expand Down
10 changes: 5 additions & 5 deletions src/libcollections/dlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1322,7 +1322,7 @@ mod tests {

#[bench]
fn bench_collect_into(b: &mut test::Bencher) {
let v = &[0i, ..64];
let v = &[0i; 64];
b.iter(|| {
let _: DList<int> = v.iter().map(|x| *x).collect();
})
Expand Down Expand Up @@ -1384,31 +1384,31 @@ mod tests {

#[bench]
fn bench_iter(b: &mut test::Bencher) {
let v = &[0i, ..128];
let v = &[0i; 128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().count() == 128);
})
}
#[bench]
fn bench_iter_mut(b: &mut test::Bencher) {
let v = &[0i, ..128];
let v = &[0i; 128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter_mut().count() == 128);
})
}
#[bench]
fn bench_iter_rev(b: &mut test::Bencher) {
let v = &[0i, ..128];
let v = &[0i; 128];
let m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter().rev().count() == 128);
})
}
#[bench]
fn bench_iter_mut_rev(b: &mut test::Bencher) {
let v = &[0i, ..128];
let v = &[0i; 128];
let mut m: DList<int> = v.iter().map(|&x|x).collect();
b.iter(|| {
assert!(m.iter_mut().rev().count() == 128);
Expand Down
27 changes: 14 additions & 13 deletions src/libcollections/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ pub trait SliceExt<T> for Sized? {
fn get_mut(&mut self, index: uint) -> Option<&mut T>;

/// Work with `self` as a mut slice.
/// Primarily intended for getting a &mut [T] from a [T, ..N].
/// Primarily intended for getting a &mut [T] from a [T; N].
#[stable]
fn as_mut_slice(&mut self) -> &mut [T];

Expand Down Expand Up @@ -861,6 +861,7 @@ pub trait CloneSliceExt<T> for Sized? {
fn clone_from_slice(&mut self, &[T]) -> uint;
}


#[unstable = "trait is unstable"]
impl<T: Clone> CloneSliceExt<T> for [T] {
/// Returns a copy of `v`.
Expand Down Expand Up @@ -1482,14 +1483,14 @@ mod tests {

#[test]
fn test_is_empty() {
let xs: [int, ..0] = [];
let xs: [int; 0] = [];
assert!(xs.is_empty());
assert!(![0i].is_empty());
}

#[test]
fn test_len_divzero() {
type Z = [i8, ..0];
type Z = [i8; 0];
let v0 : &[Z] = &[];
let v1 : &[Z] = &[[]];
let v2 : &[Z] = &[[], []];
Expand Down Expand Up @@ -1856,7 +1857,7 @@ mod tests {
#[test]
fn test_permutations() {
{
let v: [int, ..0] = [];
let v: [int; 0] = [];
let mut it = v.permutations();
let (min_size, max_opt) = it.size_hint();
assert_eq!(min_size, 1);
Expand Down Expand Up @@ -2059,7 +2060,7 @@ mod tests {
}

// shouldn't panic
let mut v: [uint, .. 0] = [];
let mut v: [uint; 0] = [];
v.sort();

let mut v = [0xDEADBEEFu];
Expand All @@ -2071,7 +2072,7 @@ mod tests {
fn test_sort_stability() {
for len in range(4i, 25) {
for _ in range(0u, 10) {
let mut counts = [0i, .. 10];
let mut counts = [0i; 10];

// create a vector like [(6, 1), (5, 1), (6, 2), ...],
// where the first item of each tuple is random, but
Expand Down Expand Up @@ -2116,28 +2117,28 @@ mod tests {

#[test]
fn test_concat() {
let v: [Vec<int>, ..0] = [];
let v: [Vec<int>; 0] = [];
let c: Vec<int> = v.concat();
assert_eq!(c, []);
let d: Vec<int> = [vec![1i], vec![2i,3i]].concat();
assert_eq!(d, vec![1i, 2, 3]);

let v: [&[int], ..2] = [&[1], &[2, 3]];
let v: [&[int]; 2] = [&[1], &[2, 3]];
assert_eq!(v.connect(&0), vec![1i, 0, 2, 3]);
let v: [&[int], ..3] = [&[1i], &[2], &[3]];
let v: [&[int]; 3] = [&[1i], &[2], &[3]];
assert_eq!(v.connect(&0), vec![1i, 0, 2, 0, 3]);
}

#[test]
fn test_connect() {
let v: [Vec<int>, ..0] = [];
let v: [Vec<int>; 0] = [];
assert_eq!(v.connect_vec(&0), vec![]);
assert_eq!([vec![1i], vec![2i, 3]].connect_vec(&0), vec![1, 0, 2, 3]);
assert_eq!([vec![1i], vec![2i], vec![3i]].connect_vec(&0), vec![1, 0, 2, 0, 3]);

let v: [&[int], ..2] = [&[1], &[2, 3]];
let v: [&[int]; 2] = [&[1], &[2, 3]];
assert_eq!(v.connect_vec(&0), vec![1, 0, 2, 3]);
let v: [&[int], ..3] = [&[1], &[2], &[3]];
let v: [&[int]; 3] = [&[1], &[2], &[3]];
assert_eq!(v.connect_vec(&0), vec![1, 0, 2, 0, 3]);
}

Expand Down Expand Up @@ -2710,7 +2711,7 @@ mod tests {
}
assert_eq!(cnt, 11);

let xs: [Foo, ..3] = [Foo, Foo, Foo];
let xs: [Foo; 3] = [Foo, Foo, Foo];
cnt = 0;
for f in xs.iter() {
assert!(*f == Foo);
Expand Down
8 changes: 4 additions & 4 deletions src/libcollections/str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2517,7 +2517,7 @@ mod tests {

#[test]
fn test_chars_decoding() {
let mut bytes = [0u8, ..4];
let mut bytes = [0u8; 4];
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
let len = c.encode_utf8(&mut bytes).unwrap_or(0);
let s = ::core::str::from_utf8(bytes[..len]).unwrap();
Expand All @@ -2529,7 +2529,7 @@ mod tests {

#[test]
fn test_chars_rev_decoding() {
let mut bytes = [0u8, ..4];
let mut bytes = [0u8; 4];
for c in range(0u32, 0x110000).filter_map(|c| ::core::char::from_u32(c)) {
let len = c.encode_utf8(&mut bytes).unwrap_or(0);
let s = ::core::str::from_utf8(bytes[..len]).unwrap();
Expand Down Expand Up @@ -2743,7 +2743,7 @@ mod tests {
use core::iter::order;
// official Unicode test data
// from http://www.unicode.org/Public/UCD/latest/ucd/auxiliary/GraphemeBreakTest.txt
let test_same: [(_, &[_]), .. 325] = [
let test_same: [(_, &[_]); 325] = [
("\u{20}\u{20}", &["\u{20}", "\u{20}"]),
("\u{20}\u{308}\u{20}", &["\u{20}\u{308}", "\u{20}"]),
("\u{20}\u{D}", &["\u{20}", "\u{D}"]),
Expand Down Expand Up @@ -3075,7 +3075,7 @@ mod tests {
("\u{646}\u{200D}\u{20}", &["\u{646}\u{200D}", "\u{20}"]),
];

let test_diff: [(_, &[_], &[_]), .. 23] = [
let test_diff: [(_, &[_], &[_]); 23] = [
("\u{20}\u{903}", &["\u{20}\u{903}"], &["\u{20}", "\u{903}"]), ("\u{20}\u{308}\u{903}",
&["\u{20}\u{308}\u{903}"], &["\u{20}\u{308}", "\u{903}"]), ("\u{D}\u{308}\u{903}",
&["\u{D}", "\u{308}\u{903}"], &["\u{D}", "\u{308}", "\u{903}"]), ("\u{A}\u{308}\u{903}",
Expand Down
2 changes: 1 addition & 1 deletion src/libcollections/string.rs
Original file line number Diff line number Diff line change
Expand Up @@ -675,7 +675,7 @@ impl String {
assert!(idx <= len);
assert!(self.is_char_boundary(idx));
self.vec.reserve(4);
let mut bits = [0, ..4];
let mut bits = [0; 4];
let amt = ch.encode_utf8(&mut bits).unwrap();

unsafe {
Expand Down
38 changes: 19 additions & 19 deletions src/libcore/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,33 +26,33 @@ macro_rules! array_impls {
($($N:expr)+) => {
$(
#[stable]
impl<T:Copy> Clone for [T, ..$N] {
fn clone(&self) -> [T, ..$N] {
impl<T:Copy> Clone for [T; $N] {
fn clone(&self) -> [T; $N] {
*self
}
}

#[unstable = "waiting for Show to stabilize"]
impl<T:fmt::Show> fmt::Show for [T, ..$N] {
impl<T:fmt::Show> fmt::Show for [T; $N] {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fmt::Show::fmt(&self[], f)
}
}

#[stable]
impl<A, B> PartialEq<[B, ..$N]> for [A, ..$N] where A: PartialEq<B> {
impl<A, B> PartialEq<[B; $N]> for [A; $N] where A: PartialEq<B> {
#[inline]
fn eq(&self, other: &[B, ..$N]) -> bool {
fn eq(&self, other: &[B; $N]) -> bool {
self[] == other[]
}
#[inline]
fn ne(&self, other: &[B, ..$N]) -> bool {
fn ne(&self, other: &[B; $N]) -> bool {
self[] != other[]
}
}

#[stable]
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A, ..$N] where
impl<'a, A, B, Rhs> PartialEq<Rhs> for [A; $N] where
A: PartialEq<B>,
Rhs: Deref<[B]>,
{
Expand All @@ -63,47 +63,47 @@ macro_rules! array_impls {
}

#[stable]
impl<'a, A, B, Lhs> PartialEq<[B, ..$N]> for Lhs where
impl<'a, A, B, Lhs> PartialEq<[B; $N]> for Lhs where
A: PartialEq<B>,
Lhs: Deref<[A]>
{
#[inline(always)]
fn eq(&self, other: &[B, ..$N]) -> bool { PartialEq::eq(&**self, other[]) }
fn eq(&self, other: &[B; $N]) -> bool { PartialEq::eq(&**self, other[]) }
#[inline(always)]
fn ne(&self, other: &[B, ..$N]) -> bool { PartialEq::ne(&**self, other[]) }
fn ne(&self, other: &[B; $N]) -> bool { PartialEq::ne(&**self, other[]) }
}

#[stable]
impl<T:Eq> Eq for [T, ..$N] { }
impl<T:Eq> Eq for [T; $N] { }

#[stable]
impl<T:PartialOrd> PartialOrd for [T, ..$N] {
impl<T:PartialOrd> PartialOrd for [T; $N] {
#[inline]
fn partial_cmp(&self, other: &[T, ..$N]) -> Option<Ordering> {
fn partial_cmp(&self, other: &[T; $N]) -> Option<Ordering> {
PartialOrd::partial_cmp(&self[], &other[])
}
#[inline]
fn lt(&self, other: &[T, ..$N]) -> bool {
fn lt(&self, other: &[T; $N]) -> bool {
PartialOrd::lt(&self[], &other[])
}
#[inline]
fn le(&self, other: &[T, ..$N]) -> bool {
fn le(&self, other: &[T; $N]) -> bool {
PartialOrd::le(&self[], &other[])
}
#[inline]
fn ge(&self, other: &[T, ..$N]) -> bool {
fn ge(&self, other: &[T; $N]) -> bool {
PartialOrd::ge(&self[], &other[])
}
#[inline]
fn gt(&self, other: &[T, ..$N]) -> bool {
fn gt(&self, other: &[T; $N]) -> bool {
PartialOrd::gt(&self[], &other[])
}
}

#[stable]
impl<T:Ord> Ord for [T, ..$N] {
impl<T:Ord> Ord for [T; $N] {
#[inline]
fn cmp(&self, other: &[T, ..$N]) -> Ordering {
fn cmp(&self, other: &[T; $N]) -> Ordering {
Ord::cmp(&self[], &other[])
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/float.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ pub fn float_to_str_bytes_common<T: Float, U, F>(
// For an f64 the exponent is in the range of [-1022, 1023] for base 2, so
// we may have up to that many digits. Give ourselves some extra wiggle room
// otherwise as well.
let mut buf = [0u8, ..1536];
let mut buf = [0u8; 1536];
let mut end = 0;
let radix_gen: T = cast(radix as int).unwrap();

Expand Down
6 changes: 3 additions & 3 deletions src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ impl<'a> Formatter<'a> {
// Writes the sign if it exists, and then the prefix if it was requested
let write_prefix = |&: f: &mut Formatter| {
for c in sign.into_iter() {
let mut b = [0, ..4];
let mut b = [0; 4];
let n = c.encode_utf8(&mut b).unwrap_or(0);
try!(f.buf.write(b[..n]));
}
Expand Down Expand Up @@ -505,7 +505,7 @@ impl<'a> Formatter<'a> {
rt::AlignCenter => (padding / 2, (padding + 1) / 2),
};

let mut fill = [0u8, ..4];
let mut fill = [0u8; 4];
let len = self.fill.encode_utf8(&mut fill).unwrap_or(0);

for _ in range(0, pre_pad) {
Expand Down Expand Up @@ -606,7 +606,7 @@ impl Show for char {
fn fmt(&self, f: &mut Formatter) -> Result {
use char::Char;

let mut utf8 = [0u8, ..4];
let mut utf8 = [0u8; 4];
let amt = self.encode_utf8(&mut utf8).unwrap_or(0);
let s: &str = unsafe { mem::transmute(utf8[..amt]) };
Show::fmt(s, f)
Expand Down
2 changes: 1 addition & 1 deletion src/libcore/fmt/num.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ trait GenericRadix {
// characters for a base 2 number.
let zero = Int::zero();
let is_positive = x >= zero;
let mut buf = [0u8, ..64];
let mut buf = [0u8; 64];
let mut curr = buf.len();
let base = cast(self.base()).unwrap();
if is_positive {
Expand Down
Loading