-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b99a70
commit 6767713
Showing
57 changed files
with
368 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "higher-order-functions" | ||
version = "0.1.0" | ||
authors = ["Aurélien DESBRIÈRES <[email protected]>"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* Aurélien DESBRIÈRES | ||
aurelien(at)hackers(dot)camp | ||
License GNU GPL latest */ | ||
|
||
// Rust experimentations | ||
// HOF - Higher Order Functions in Rust | ||
|
||
fn is_odd(n: u32) -> bool { | ||
n % 2 == 1 | ||
} | ||
|
||
fn main() { | ||
println!("Find the sum of all the squared odd numbers under 1000"); | ||
let upper = 1000; | ||
|
||
// Imperative approach | ||
// Declare accumulator varialbe | ||
let mut acc = 0; | ||
// Iterate: 0, 1, 2, ... to infinity | ||
for n in 0.. { | ||
// Square the number | ||
let n_squared = n * n; | ||
|
||
if n_squared >= upper { | ||
// Break loop if exceeded the upper limit | ||
break; | ||
} else if is_odd(n_squared) { | ||
// Accumulate value, if it's odd | ||
acc += n_squared; | ||
} | ||
} | ||
println!("imperative style: {}", acc); | ||
|
||
// Functional approach | ||
let sum_of_squared_odd_numbers: u32 = | ||
(0..).map(|n| n * n) // All natural numbers squared | ||
.take_while(|&n| n < upper) // Below upper limit | ||
.filter(|&n| is_odd(n)) // That are odd | ||
.fold(0, |sum, i| sum + i); // Sum them | ||
println!("functional style: {}", sum_of_squared_odd_numbers); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
...print/higher-order-functions-1ffddf74c6506548/bin-higher_order_functions-1ffddf74c6506548
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
4b92e2d5830d32b1 |
1 change: 1 addition & 0 deletions
1
.../higher-order-functions-1ffddf74c6506548/bin-higher_order_functions-1ffddf74c6506548.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"rustc":7337822701906583572,"target":17663985639602434515,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1491816739,952247649],[47,104,111,109,101,47,97,117,114,101,108,105,101,110,47,103,105,116,47,114,117,115,116,47,104,105,103,104,101,114,45,111,114,100,101,114,45,102,117,110,99,116,105,111,110,115,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,104,105,103,104,101,114,45,111,114,100,101,114,45,102,117,110,99,116,105,111,110,115,45,49,102,102,100,100,102,55,52,99,54,53,48,54,53,52,56,47,100,101,112,45,98,105,110,45,104,105,103,104,101,114,95,111,114,100,101,114,95,102,117,110,99,116,105,111,110,115,45,49,102,102,100,100,102,55,52,99,54,53,48,54,53,52,56]]},"features":"None","deps":[],"rustflags":[]} |
Binary file added
BIN
+300 Bytes
...t/higher-order-functions-1ffddf74c6506548/dep-bin-higher_order_functions-1ffddf74c6506548
Binary file not shown.
Binary file added
BIN
+3.41 MB
higher-order-functions/target/debug/deps/higher_order_functions-1ffddf74c6506548
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/home/aurelien/git/rust/higher-order-functions/target/debug/higher-order-functions: /home/aurelien/git/rust/higher-order-functions/src/main.rs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "iterator-find" | ||
version = "0.1.0" | ||
authors = ["Aurélien DESBRIÈRES <[email protected]>"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/* Aurélien DESBRIÈRES | ||
aurelien(at)hackers(dot)camp | ||
License GNU GPL latest */ | ||
|
||
// Rust experimentations | ||
// Iterator find in Rust | ||
|
||
fn main() { | ||
let vec1 = vec![1, 2, 3]; | ||
let vec2 = vec![4, 5, 6]; | ||
|
||
// `iter()` for vec yields `&i32`. | ||
let mut iter = vec1.iter(); | ||
// `into_iter()` for vec yields `i32`. | ||
let mut into_iter = vec2.into_iter(); | ||
|
||
// A reference to what is yielded is `&&i32`. Destructure to `i32`. | ||
println!("Find 2 in vec1: {:?}", iter .find(|&&x| x == 2)); | ||
// A reference to what is yielded is `&i32`. Destructure to `i32`. | ||
println!("Find 2 in vec2: {:?}", into_iter.find(| &x| x == 2)); | ||
|
||
let array1 = [1, 2, 3]; | ||
let array2 = [4, 5, 6]; | ||
|
||
// `iter()` for arrays yields `&i32` | ||
println!("Find 2 in array1: {:?}", array1.iter() .find(|&&x| x == 2)); | ||
// `into_iter()` for arrays unusually yields `&i32` | ||
println!("Find 2 in array2: {:?}", array2.into_iter().find(|&&x| x == 2)); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
...rget/debug/.fingerprint/iterator-find-70176a37c36c78ac/bin-iterator_find-70176a37c36c78ac
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
67015548b56cb8b2 |
1 change: 1 addition & 0 deletions
1
...debug/.fingerprint/iterator-find-70176a37c36c78ac/bin-iterator_find-70176a37c36c78ac.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"rustc":7337822701906583572,"target":9291745995434966666,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1491816056,792262795],[47,104,111,109,101,47,97,117,114,101,108,105,101,110,47,103,105,116,47,114,117,115,116,47,105,116,101,114,97,116,111,114,45,102,105,110,100,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,105,116,101,114,97,116,111,114,45,102,105,110,100,45,55,48,49,55,54,97,51,55,99,51,54,99,55,56,97,99,47,100,101,112,45,98,105,110,45,105,116,101,114,97,116,111,114,95,102,105,110,100,45,55,48,49,55,54,97,51,55,99,51,54,99,55,56,97,99]]},"features":"None","deps":[],"rustflags":[]} |
Binary file added
BIN
+255 Bytes
.../debug/.fingerprint/iterator-find-70176a37c36c78ac/dep-bin-iterator_find-70176a37c36c78ac
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/home/aurelien/git/rust/iterator-find/target/debug/iterator-find: /home/aurelien/git/rust/iterator-find/src/main.rs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "iterator" | ||
version = "0.1.0" | ||
authors = ["Aurélien DESBRIÈRES <[email protected]>"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
stty tab0 -inlcr -onlcr -echo kill '^U' erase '^H' | ||
echo foo | ||
PS1=///7ded89db0b4ea90cff909b1ab08f0585\#\$ PS2='' PS3='' PROMPT_COMMAND='' | ||
echo \"`uname -sr`\" 2>/dev/null; echo tramp_exit_status $? | ||
echo foo ; echo bar | ||
set +o vi +o emacs | ||
PATH=/bin:/usr/bin:/sbin:/usr/sbin:/usr/local/bin:/usr/local/sbin:/opt/bin; export PATH | ||
mesg n 2>/dev/null; biff n 2>/dev/null | ||
stty iutf8 2>/dev/null | ||
echo \"`tty`\" 2>/dev/null; echo tramp_exit_status $? | ||
while read var val; do export $var=$val; done <<'123b1d2f7d58a818fa70d25e2eff25fd' | ||
PAGER cat | ||
INSIDE_EMACS '25.1.1,tramp:2.2.13.25.1' | ||
TERM dumb | ||
LC_CTYPE '' | ||
TMOUT 0 | ||
LC_ALL en_US.utf8 | ||
123b1d2f7d58a818fa70d25e2eff25fd | ||
|
||
unset correct autocorrect MAILPATH MAILCHECK MAIL HISTORY CDPATH | ||
cd /home/aurelien/git/rust/iterator/src/ && exec env PS1\=/sshx\:aurelien\@icebreaker\:/home/aurelien/git/rust/iterator/src/\ \#\$\ TERMCAP\= COLUMNS\=118 INSIDE_EMACS\=25.1.1\,comint /bin/bash --noediting -i |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* Aurélien DESBRIÈRES | ||
aurelien(at)hackers(dot)camp | ||
License GNU GPL latest */ | ||
|
||
// Rust experimentations | ||
// Iterator in Rust | ||
|
||
fn main() { | ||
let vec1 = vec![1, 2, 3]; | ||
let vec2 = vec![4, 5, 6]; | ||
|
||
// `iter()` for vec yields `&i32, Destructure to `i32` | ||
println!("2 in vec1: {}", vec1.iter() .any(|&x| x == 2)); | ||
// `into_iter()` for vecs yields `i32`, No destructuring required, | ||
println!("2 in vec2: {}", vec2.into_iter().any(| x| x == 2)); | ||
|
||
let array1 = [1, 2, 3]; | ||
let array2 = [4, 5, 6]; | ||
|
||
// `iter()` for arrays yield `&i32`. | ||
println!("2 in array1: {}", array1.iter() .any(|&x| x == 2)); | ||
// `into_iter()` for arrays unnusually yields `&i32` | ||
println!("2 in array2; {}", array2.into_iter().any(|&x| x == 2)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
iterator/target/debug/.fingerprint/iterator-2a85bff397dbd843/bin-iterator-2a85bff397dbd843
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
64863de95bac730e |
1 change: 1 addition & 0 deletions
1
...or/target/debug/.fingerprint/iterator-2a85bff397dbd843/bin-iterator-2a85bff397dbd843.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"rustc":7337822701906583572,"target":7291426499645126153,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1491501286,729241359],[47,104,111,109,101,47,97,117,114,101,108,105,101,110,47,103,105,116,47,114,117,115,116,47,105,116,101,114,97,116,111,114,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,105,116,101,114,97,116,111,114,45,50,97,56,53,98,102,102,51,57,55,100,98,100,56,52,51,47,100,101,112,45,98,105,110,45,105,116,101,114,97,116,111,114,45,50,97,56,53,98,102,102,51,57,55,100,98,100,56,52,51]]},"features":"None","deps":[],"rustflags":[]} |
Binary file added
BIN
+230 Bytes
...tor/target/debug/.fingerprint/iterator-2a85bff397dbd843/dep-bin-iterator-2a85bff397dbd843
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/home/aurelien/git/rust/iterator/target/debug/iterator: /home/aurelien/git/rust/iterator/src/main.rs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "modules-struct-visibility" | ||
version = "0.1.0" | ||
authors = ["Aurélien DESBRIÈRES <[email protected]>"] | ||
|
||
[dependencies] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/* Aurélien DESBRIÈRES | ||
aurelien(at)hackers(dot)camp | ||
License GNU GPL latest */ | ||
|
||
// Rust experimentations | ||
// Modules Struct Visibility in Rust | ||
|
||
mod my { | ||
// A public struct with a public field of generic type `T` | ||
pub struct WhiteBox<T> { | ||
pub contents: T, | ||
} | ||
|
||
// A public struct with a private field of generic type `T` | ||
#[allow(dead_code)] | ||
pub struct BlackBox<T> { | ||
contents: T, | ||
} | ||
|
||
impl<T> BlackBox<T> { | ||
// A public constructor method | ||
pub fn new(contents: T) -> BlackBox<T> { | ||
BlackBox { | ||
contents: contents, | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn main() { | ||
// Public structs with public fields can be constructed as usual | ||
let white_box = my::WhiteBox { contents: "public information" }; | ||
|
||
// and their fields can be normally accessed. | ||
println!("The white box contains: {}", white_box.contents); | ||
|
||
// Public structs with private fields cannot be constructed using field names. | ||
// Error! `BlackBox` has private fields | ||
//let black_box = my::BlackBox { contents: "classified information" }; | ||
// TODO ^ Try uncommenting this line | ||
|
||
// However, structs with private fields can be created using | ||
// public constructors | ||
let _black_box = my::BlackBox::new("classified information"); | ||
|
||
// and the private fields of a public struct cannot be accessed. | ||
// Error! The `contents` field is private | ||
println!("The black box contains: {}", _black_box.contents); | ||
// TODO ^ Try uncommenting this line | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
fn main() { | ||
println!("Hello, world!"); | ||
} |
Empty file.
1 change: 1 addition & 0 deletions
1
...modules-struct-visibility-a5e6012477b2eda2/bin-modules_struct_visibility-a5e6012477b2eda2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
d1e428418f7bfdea |
1 change: 1 addition & 0 deletions
1
...es-struct-visibility-a5e6012477b2eda2/bin-modules_struct_visibility-a5e6012477b2eda2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"rustc":7337822701906583572,"target":17217705447684493301,"profile":14528549471338536373,"local":{"variant":"MtimeBased","fields":[[1491940292,189508453],[47,104,111,109,101,47,97,117,114,101,108,105,101,110,47,103,105,116,47,114,117,115,116,47,109,111,100,117,108,101,115,45,115,116,114,117,99,116,45,118,105,115,105,98,105,108,105,116,121,47,116,97,114,103,101,116,47,100,101,98,117,103,47,46,102,105,110,103,101,114,112,114,105,110,116,47,109,111,100,117,108,101,115,45,115,116,114,117,99,116,45,118,105,115,105,98,105,108,105,116,121,45,97,53,101,54,48,49,50,52,55,55,98,50,101,100,97,50,47,100,101,112,45,98,105,110,45,109,111,100,117,108,101,115,95,115,116,114,117,99,116,95,118,105,115,105,98,105,108,105,116,121,45,97,53,101,54,48,49,50,52,55,55,98,50,101,100,97,50]]},"features":"None","deps":[],"rustflags":[]} |
Binary file added
BIN
+315 Bytes
...les-struct-visibility-a5e6012477b2eda2/dep-bin-modules_struct_visibility-a5e6012477b2eda2
Binary file not shown.
5 changes: 5 additions & 0 deletions
5
modules-struct-visibility/target/debug/deps/modules_struct_visibility-a5e6012477b2eda2.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
/home/aurelien/git/rust/modules-struct-visibility/target/debug/deps/modules_struct_visibility-a5e6012477b2eda2: src/main.rs | ||
|
||
/home/aurelien/git/rust/modules-struct-visibility/target/debug/deps/modules_struct_visibility-a5e6012477b2eda2.d: src/main.rs | ||
|
||
src/main.rs: |
Binary file not shown.
1 change: 1 addition & 0 deletions
1
modules-struct-visibility/target/debug/modules-struct-visibility.d
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
/home/aurelien/git/rust/modules-struct-visibility/target/debug/modules-struct-visibility: /home/aurelien/git/rust/modules-struct-visibility/src/main.rs |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[package] | ||
name = "modules-visibility" | ||
version = "0.1.0" | ||
authors = ["Aurélien DESBRIÈRES <[email protected]>"] | ||
|
||
[dependencies] |
Oops, something went wrong.