mirror of https://github.com/rust-lang/rustlings
Clean up and unify exercises
parent
cb9f1ac9ce
commit
2f810a4da6
@ -1,8 +1,3 @@
|
|||||||
// functions1.rs
|
|
||||||
//
|
|
||||||
// Execute `rustlings hint functions1` or use the `hint` watch subcommand for a
|
|
||||||
// hint.
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
call_me();
|
call_me();
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,19 @@
|
|||||||
// primitive_types4.rs
|
|
||||||
//
|
|
||||||
// Get a slice out of Array a where the ??? is so that the test passes.
|
// Get a slice out of Array a where the ??? is so that the test passes.
|
||||||
//
|
|
||||||
// Execute `rustlings hint primitive_types4` or use the `hint` watch subcommand
|
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn slice_out_of_array() {
|
mod tests {
|
||||||
let a = [1, 2, 3, 4, 5];
|
use super::*;
|
||||||
|
|
||||||
let nice_slice = ???
|
#[test]
|
||||||
|
fn slice_out_of_array() {
|
||||||
|
let a = [1, 2, 3, 4, 5];
|
||||||
|
|
||||||
assert_eq!([2, 3, 4], nice_slice)
|
let nice_slice = ???
|
||||||
|
|
||||||
|
assert_eq!([2, 3, 4], nice_slice)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,21 @@
|
|||||||
// primitive_types6.rs
|
|
||||||
//
|
|
||||||
// Use a tuple index to access the second element of `numbers`. You can put the
|
// Use a tuple index to access the second element of `numbers`. You can put the
|
||||||
// expression for the second element where ??? is so that the test passes.
|
// expression for the second element where ??? is so that the test passes.
|
||||||
//
|
|
||||||
// Execute `rustlings hint primitive_types6` or use the `hint` watch subcommand
|
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// You can optionally experiment here.
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[cfg(test)]
|
||||||
fn indexing_tuple() {
|
mod tests {
|
||||||
let numbers = (1, 2, 3);
|
use super::*;
|
||||||
// Replace below ??? with the tuple indexing syntax.
|
|
||||||
let second = ???;
|
|
||||||
|
|
||||||
assert_eq!(2, second,
|
#[test]
|
||||||
"This is not the 2nd number in the tuple!")
|
fn indexing_tuple() {
|
||||||
|
let numbers = (1, 2, 3);
|
||||||
|
// Replace below ??? with the tuple indexing syntax.
|
||||||
|
let second = ???;
|
||||||
|
|
||||||
|
assert_eq!(2, second,
|
||||||
|
"This is not the 2nd number in the tuple!")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,21 +1,25 @@
|
|||||||
// move_semantics1.rs
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
//
|
let vec = vec;
|
||||||
// Execute `rustlings hint move_semantics1` or use the `hint` watch subcommand
|
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
#[test]
|
vec.push(88);
|
||||||
fn main() {
|
|
||||||
let vec0 = vec![22, 44, 66];
|
|
||||||
|
|
||||||
let vec1 = fill_vec(vec0);
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
#[cfg(test)]
|
||||||
let vec = vec;
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
vec.push(88);
|
#[test]
|
||||||
|
fn move_semantics1() {
|
||||||
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
vec
|
let vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,22 +1,26 @@
|
|||||||
// move_semantics3.rs
|
|
||||||
//
|
|
||||||
// Make me compile without adding new lines -- just changing existing lines! (no
|
// Make me compile without adding new lines -- just changing existing lines! (no
|
||||||
// lines with multiple semicolons necessary!)
|
// lines with multiple semicolons necessary!)
|
||||||
//
|
|
||||||
// Execute `rustlings hint move_semantics3` or use the `hint` watch subcommand
|
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
#[test]
|
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
||||||
fn main() {
|
vec.push(88);
|
||||||
let vec0 = vec![22, 44, 66];
|
|
||||||
|
|
||||||
let vec1 = fill_vec(vec0);
|
vec
|
||||||
|
}
|
||||||
|
|
||||||
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
fn main() {
|
||||||
|
// You can optionally experiment here.
|
||||||
}
|
}
|
||||||
|
|
||||||
fn fill_vec(vec: Vec<i32>) -> Vec<i32> {
|
#[cfg(test)]
|
||||||
vec.push(88);
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
vec
|
#[test]
|
||||||
|
fn move_semantics3() {
|
||||||
|
let vec0 = vec![22, 44, 66];
|
||||||
|
|
||||||
|
let vec1 = fill_vec(vec0);
|
||||||
|
|
||||||
|
assert_eq!(vec1, vec![22, 44, 66, 88]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
// move_semantics5.rs
|
// Make me compile only by reordering the lines in the test, but without adding,
|
||||||
//
|
|
||||||
// Make me compile only by reordering the lines in `main()`, but without adding,
|
|
||||||
// changing or removing any of them.
|
// changing or removing any of them.
|
||||||
//
|
|
||||||
// Execute `rustlings hint move_semantics5` or use the `hint` watch subcommand
|
|
||||||
// for a hint.
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut x = 100;
|
// You can optionally experiment here.
|
||||||
let y = &mut x;
|
}
|
||||||
let z = &mut x;
|
|
||||||
*y += 100;
|
#[cfg(test)]
|
||||||
*z += 1000;
|
mod tests {
|
||||||
assert_eq!(x, 1200);
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn move_semantics5() {
|
||||||
|
let mut x = 100;
|
||||||
|
let y = &mut x;
|
||||||
|
let z = &mut x;
|
||||||
|
*y += 100;
|
||||||
|
*z += 1000;
|
||||||
|
assert_eq!(x, 1200);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,24 +1,28 @@
|
|||||||
// iterators1.rs
|
|
||||||
//
|
|
||||||
// When performing operations on elements within a collection, iterators are
|
// When performing operations on elements within a collection, iterators are
|
||||||
// essential. This module helps you get familiar with the structure of using an
|
// essential. This module helps you get familiar with the structure of using an
|
||||||
// iterator and how to go through elements within an iterable collection.
|
// iterator and how to go through elements within an iterable collection.
|
||||||
//
|
//
|
||||||
// Make me compile by filling in the `???`s
|
// Make me compile by filling in the `???`s
|
||||||
//
|
|
||||||
// Execute `rustlings hint iterators1` or use the `hint` watch subcommand for a
|
|
||||||
// hint.
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
// You can optionally experiment here.
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn iterators() {
|
||||||
|
let my_fav_fruits = vec!["banana", "custard apple", "avocado", "peach", "raspberry"];
|
||||||
|
|
||||||
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
let mut my_iterable_fav_fruits = ???; // TODO: Step 1
|
||||||
|
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"banana"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 2
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"avocado"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 3
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
assert_eq!(my_iterable_fav_fruits.next(), Some(&"raspberry"));
|
||||||
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
assert_eq!(my_iterable_fav_fruits.next(), ???); // TODO: Step 4
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue