Tuples are a lightweight way to group a fixed set of arbitrary types of data together. A tuple doesn't have
a particular name; naming a data structure turns it into a struct
. A tuple's fields don't have names;
they are accessed by means of destructuring or by position.
Tuples are always created with a tuple expression:
// pointless but legal
let unit = ();
// single element
let single_element = ("note the comma",);
// two element
let two_element = (123, "elements can be of differing types");
Tuples can have an arbitrary number of elements.
It is possible to access the elements of a tuple by destructuring. This just means assigning variable names to the individual elements of the tuple, consuming it.
let (elem1, _elem2) = two_element;
assert_eq!(elem1, 123);
It is also possible to access the elements of a tuple by numeric positional index. Indexing, as always, begins at 0.
let notation = single_element.0;
assert_eq!(notation, "note the comma");
You will also be asked to work with tuple structs. Like normal structs, these are named types; unlike normal structs, they have anonymous fields. Their syntax is very similar to normal tuple syntax. It is legal to use both destructuring and positional access.
struct TupleStruct(u8, i32);
let my_tuple_struct = TupleStruct(123, -321);
let neg = my_tuple_struct.1;
let TupleStruct(byte, _) = my_tuple_struct;
assert_eq!(neg, -321);
assert_eq!(byte, 123);
All fields of anonymous tuples are always public. However, fields of tuple structs have individual
visibility which defaults to private, just like fields of standard structs. You can make the fields
public with the pub
modifier, just as in a standard struct.
// fails due to private fields
mod tuple { pub struct TupleStruct(u8, i32); }
fn main() { let _my_tuple_struct = tuple::TupleStruct(123, -321); }
// succeeds: fields are public
mod tuple { pub struct TupleStruct(pub u8, pub i32); }
fn main() { let _my_tuple_struct = tuple::TupleStruct(123, -321); }
You are working on a game targeting a low-power embedded system and need to write several convenience functions which will be used by other parts of the game.
A quotient is the output of a division.
fn divmod(dividend: i16, divisor: i16) -> (i16, i16)
Example:
assert_eq!(divmod(10, 3), (3, 1));
This will be helpful to enable a screen-buffer optimization, your boss promises.
Iterators are items which expose the methods defined by the Iterator
trait. That documentation is fairly extensive, because they offer many methods; here are the most relevant properties:
enumerate
method which returns a tuple (i, val)
for each valuefilter
method which uses a closure to determine whether to yield an element of the iteratormap
method which uses a closure to modify elements of the iteratorBecause your function can run on any kind of iterator, it uses impl
to signify that this is a trait instance instead of a simple item. Likewise, the <Item=T>
syntax just means that it doesn't matter what kind of item the iterator produces; your function can produce the even elements of any iterator.
fn evens<T>(iter: impl Iterator<Item=T>) -> impl Iterator<Item=T>
Examples:
let mut even_ints = evens(0_u8..);
assert_eq!(even_ints.next(), Some(0));
assert_eq!(even_ints.next(), Some(2));
assert_eq!(even_ints.next(), Some(4));
assert_eq!(even_ints.next(), Some(6));
let mut evens_from_odds = evens(1_i16..);
assert_eq!(evens_from_odds.next(), Some(1));
assert_eq!(evens_from_odds.next(), Some(3));
assert_eq!(evens_from_odds.next(), Some(5));
assert_eq!(evens_from_odds.next(), Some(7));
For mapping convenience, you have a tuple struct Position
:
struct Position(i16, i16);
You need to implement a method manhattan
on Position
which returns the manhattan distance of that position from the origin (Position(0, 0)
).
impl Position {
fn manhattan(&self) -> i16
}
Example:
assert_eq!(Position(3, 4).manhattan(), 7);
Sign up to Exercism to learn and master Rust with 98 exercises, and real human mentoring, all for free.