deleted chapter 7 remains
rodzic
66ae824573
commit
5125a65e79
Plik binarny nie jest wyświetlany.
|
@ -1,23 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Implementing Commonn Macros
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
|
|
||||||
// Creating a vector
|
|
||||||
let v = vec![1, 2, 3, 4, 5];
|
|
||||||
print!("Vector :- {:?}", v);
|
|
||||||
|
|
||||||
// Macros used for testing
|
|
||||||
assert!(true);
|
|
||||||
assert_eq!(5, 3 + 2);
|
|
||||||
|
|
||||||
// assert!(5 < 3);
|
|
||||||
// assert_eq!(5, 3);
|
|
||||||
|
|
||||||
// Gives a message to panic
|
|
||||||
// panic!("oh no!");
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,49 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Implmenting designator
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
macro_rules! create_function {
|
|
||||||
// This macro takes an argument of designator `ident` and
|
|
||||||
// creates a function named `$func_name`.
|
|
||||||
// The `ident` designator is used for variable/function names.
|
|
||||||
($func_name:ident) => (
|
|
||||||
fn $func_name() {
|
|
||||||
// The `stringify!` macro converts an `ident` into a string.
|
|
||||||
println!("You called {:?}()",
|
|
||||||
stringify!($func_name))
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Create functions named `foo` and `bar` with the above macro.
|
|
||||||
create_function!(foo);
|
|
||||||
create_function!(bar);
|
|
||||||
|
|
||||||
macro_rules! print_result {
|
|
||||||
// This macro takes an expression of type `expr` and prints
|
|
||||||
// it as a string along with its result.
|
|
||||||
// The `expr` designator is used for expressions.
|
|
||||||
($expression:expr) => (
|
|
||||||
// `stringify!` will convert the expression *as it is* into a string.
|
|
||||||
println!("{:?} = {:?}",
|
|
||||||
stringify!($expression),
|
|
||||||
$expression)
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
foo();
|
|
||||||
bar();
|
|
||||||
|
|
||||||
print_result!(1u32 + 1);
|
|
||||||
|
|
||||||
// Recall that blocks are expressions too!
|
|
||||||
print_result!({
|
|
||||||
let x = 1u32;
|
|
||||||
|
|
||||||
x * x + 2 * x - 1
|
|
||||||
});
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,66 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Implement DRY
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
|
|
||||||
// Calling standard libraries
|
|
||||||
use std::ops::{Add, Mul, Sub};
|
|
||||||
|
|
||||||
macro_rules! assert_equal_len {
|
|
||||||
// The `tt` (token tree) designator is used for
|
|
||||||
// operators and tokens.
|
|
||||||
($a:ident, $b: ident, $func:ident, $op:tt) => (
|
|
||||||
assert!($a.len() == $b.len(),
|
|
||||||
"{:?}: dimension mismatch: {:?} {:?} {:?}",
|
|
||||||
stringify!($func),
|
|
||||||
($a.len(),),
|
|
||||||
stringify!($op),
|
|
||||||
($b.len(),));
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! op {
|
|
||||||
($func:ident, $bound:ident, $op:tt, $method:ident) => (
|
|
||||||
fn $func<T: $bound<T, Output=T> + Copy>(xs: &mut Vec<T>, ys: &Vec<T>) {
|
|
||||||
assert_equal_len!(xs, ys, $func, $op);
|
|
||||||
|
|
||||||
for (x, y) in xs.iter_mut().zip(ys.iter()) {
|
|
||||||
*x = $bound::$method(*x, *y);
|
|
||||||
// *x = x.$method(*y);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Implement `add_assign`, `mul_assign`, and `sub_assign` functions.
|
|
||||||
op!(add_assign, Add, +=, add);
|
|
||||||
op!(mul_assign, Mul, *=, mul);
|
|
||||||
op!(sub_assign, Sub, -=, sub);
|
|
||||||
|
|
||||||
mod test {
|
|
||||||
use std::iter;
|
|
||||||
macro_rules! test {
|
|
||||||
($func: ident, $x:expr, $y:expr, $z:expr) => {
|
|
||||||
#[test]
|
|
||||||
fn $func() {
|
|
||||||
for size in 0usize..10 {
|
|
||||||
let mut x: Vec<_> = iter::repeat($x).take(size).collect();
|
|
||||||
let y: Vec<_> = iter::repeat($y).take(size).collect();
|
|
||||||
let z: Vec<_> = iter::repeat($z).take(size).collect();
|
|
||||||
|
|
||||||
super::$func(&mut x, &y);
|
|
||||||
|
|
||||||
assert_eq!(x, z);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Test `add_assign`, `mul_assign` and `sub_assign`
|
|
||||||
test!(add_assign, 1u32, 2u32, 3u32);
|
|
||||||
test!(mul_assign, 2u32, 3u32, 6u32);
|
|
||||||
test!(sub_assign, 3u32, 2u32, 1u32);
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,20 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Building your first macro in Rust
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
// This is a simple macro named `say_hello`.
|
|
||||||
macro_rules! Welcome_RustBook {
|
|
||||||
// `()` indicates that the macro takes no argument.
|
|
||||||
() => (
|
|
||||||
// The macro will expand into the contents of this block.
|
|
||||||
println!("Welcome to Rust Cookbook!");
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
// This call will expand into `println!("Hello");`
|
|
||||||
Welcome_RustBook!()
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,15 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Implement matching
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
macro_rules! foo {
|
|
||||||
(x => $e:expr) => (println!("mode X: {}", $e));
|
|
||||||
(y => $e:expr) => (println!("mode Y: {}", $e));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
foo!(y => 3);
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,31 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Overloading Macros
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
// `test!` will compare `$left` and `$right`
|
|
||||||
// in different ways depending on how you invoke it:
|
|
||||||
macro_rules! test {
|
|
||||||
// Arguments don't need to be separated by a comma.
|
|
||||||
// Any template can be used!
|
|
||||||
($left:expr; and $right:expr) => (
|
|
||||||
println!("{:?} and {:?} is {:?}",
|
|
||||||
stringify!($left),
|
|
||||||
stringify!($right),
|
|
||||||
$left && $right)
|
|
||||||
);
|
|
||||||
// ^ each arm must end with a semicolon.
|
|
||||||
($left:expr; or $right:expr) => (
|
|
||||||
println!("{:?} or {:?} is {:?}",
|
|
||||||
stringify!($left),
|
|
||||||
stringify!($right),
|
|
||||||
$left || $right)
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
test!(1i32 + 1 == 2i32; and 2i32 * 2 == 4i32);
|
|
||||||
test!(true; or false);
|
|
||||||
}
|
|
Plik binarny nie jest wyświetlany.
|
@ -1,23 +0,0 @@
|
||||||
//-- #########################
|
|
||||||
//-- Task: Implementing repeats
|
|
||||||
//-- Author: Vigneshwer.D
|
|
||||||
//-- Version: 1.0.0
|
|
||||||
//-- Date: 28 March 17
|
|
||||||
//-- #########################
|
|
||||||
|
|
||||||
// `min!` will calculate the minimum of any number of arguments.
|
|
||||||
macro_rules! find_min {
|
|
||||||
// Base case:
|
|
||||||
($x:expr) => ($x);
|
|
||||||
// `$x` followed by at least one `$y,`
|
|
||||||
($x:expr, $($y:expr),+) => (
|
|
||||||
// Call `find_min!` on the tail `$y`
|
|
||||||
std::cmp::min($x, find_min!($($y),+))
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn main() {
|
|
||||||
println!("{}", find_min!(1u32));
|
|
||||||
println!("{}", find_min!(1u32 + 2 , 2u32));
|
|
||||||
println!("{}", find_min!(5u32, 2u32 * 3, 4u32));
|
|
||||||
}
|
|
Ładowanie…
Reference in New Issue