No moar prints.
This commit is contained in:
parent
98c7c05429
commit
02601eccf5
105
Cargo.lock
generated
105
Cargo.lock
generated
|
@ -2,6 +2,111 @@
|
|||
# It is not intended for manual editing.
|
||||
version = 3
|
||||
|
||||
[[package]]
|
||||
name = "itoa"
|
||||
version = "1.0.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "af150ab688ff2122fcef229be89cb50dd66af9e01a4ff320cc137eecc9bacc38"
|
||||
|
||||
[[package]]
|
||||
name = "jour1"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"nom",
|
||||
"serde",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
version = "2.6.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f665ee40bc4a3c5590afb1e9677db74a508659dfd71e126420da8274909a0167"
|
||||
|
||||
[[package]]
|
||||
name = "minimal-lexical"
|
||||
version = "0.2.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "68354c5c6bd36d73ff3feceb05efa59b6acb7626617f4962be322a825e61f79a"
|
||||
|
||||
[[package]]
|
||||
name = "nom"
|
||||
version = "7.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d273983c5a657a70a3e8f2a01329822f3b8c8172b73826411a55751e404a0a4a"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
"minimal-lexical",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "proc-macro2"
|
||||
version = "1.0.69"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da"
|
||||
dependencies = [
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ryu"
|
||||
version = "1.0.15"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1ad4cc8da4ef723ed60bced201181d83791ad433213d8c24efffda1eec85d741"
|
||||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.192"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bca2a08484b285dcb282d0f67b26cadc0df8b19f8c12502c13d966bf9482f001"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.192"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d6c7207fbec9faa48073f3e3074cbe553af6ea512d7c21ba46e434e70ea9fbc1"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.108"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3d1c7e3eac408d115102c4c24ad393e0821bb3a5df4d506a80f85f7a742a526b"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"ryu",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.39"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"unicode-ident",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "unicode-ident"
|
||||
version = "1.0.12"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
|
||||
|
|
|
@ -6,3 +6,6 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
nom = "7.1.3"
|
||||
|
|
|
@ -1,9 +1,5 @@
|
|||
fn print_fourty_two() {
|
||||
println!("42");
|
||||
}
|
||||
|
||||
fn seconds_in_a_year() {
|
||||
println!("Seconds in a year: {}", 60 * 60 * 24 * 365);
|
||||
fn seconds_in_a_year() -> u64 {
|
||||
60 * 60 * 24 * 365
|
||||
}
|
||||
|
||||
fn approx_pi() {
|
||||
|
@ -13,7 +9,6 @@ fn approx_pi() {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
print_fourty_two();
|
||||
seconds_in_a_year();
|
||||
assert!(seconds_in_a_year() > 3600);
|
||||
approx_pi();
|
||||
}
|
||||
|
|
51
src/jour1.rs
51
src/jour1.rs
|
@ -305,25 +305,25 @@ fn play_with_enum(n: Test<u64>) {
|
|||
}
|
||||
}
|
||||
|
||||
fn f_strings() {
|
||||
let n = 42;
|
||||
|
||||
println!("La réponse à la question est {n}"); // like Python f-strings.
|
||||
println!("La réposne à la question est {}", n);
|
||||
|
||||
let m = dbg!(n) + dbg!(n); // dbg! returns given value (not println!).
|
||||
assert!(m == 84);
|
||||
}
|
||||
//fn f_strings() {
|
||||
// let n = 42;
|
||||
//
|
||||
// println!("La réponse à la question est {n}"); // like Python f-strings.
|
||||
// println!("La réposne à la question est {}", n);
|
||||
//
|
||||
// let m = dbg!(n) + dbg!(n); // dbg! returns given value (not println!).
|
||||
// assert!(m == 84);
|
||||
//}
|
||||
|
||||
// Macros : functions ending with bangs.
|
||||
|
||||
fn display_difference(dur: Option<Duration>) {
|
||||
fn format_difference(dur: Option<Duration>) -> String {
|
||||
match dur {
|
||||
None => {
|
||||
println!("No duration.");
|
||||
format!("No duration.")
|
||||
}
|
||||
Some(n) => {
|
||||
println!("Duration: {n:?}")
|
||||
format!("Duration: {n:?}")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -334,8 +334,8 @@ fn durées() {
|
|||
|
||||
assert!(d2.saturating_sub(d1) == Duration::from_secs(3540));
|
||||
assert!(d1.saturating_sub(d2) == Duration::from_secs(0));
|
||||
display_difference(d1.checked_sub(d2));
|
||||
display_difference(d2.checked_sub(d1));
|
||||
assert!(format_difference(d1.checked_sub(d2)) == "No duration.");
|
||||
assert!(format_difference(d2.checked_sub(d1)) == "Duration: 3540s");
|
||||
}
|
||||
|
||||
fn ownership() {
|
||||
|
@ -489,7 +489,7 @@ fn test_points() {
|
|||
y: 0.0,
|
||||
// Also using `..p1` would get all values from p1, kind of **kwargs.
|
||||
};
|
||||
println!("{}: {}, {}", p.name, p.x, p.y);
|
||||
assert!(format!("{}: {}, {}", p.name, p.x, p.y) == "origin: 0, 0");
|
||||
|
||||
let p4: Point4D = Point4D(0.0, 0.0, 0.0, 0.0);
|
||||
assert!(p4.0 == 0.0);
|
||||
|
@ -516,10 +516,10 @@ fn test_enum() {
|
|||
|
||||
match color {
|
||||
Color::Blue => {
|
||||
println!("Is blue");
|
||||
// It is blue.
|
||||
}
|
||||
_ => {
|
||||
println!("Is not blue");
|
||||
// It is not blue.
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -530,43 +530,47 @@ fn test_enum() {
|
|||
output: String::from("Success"),
|
||||
};
|
||||
|
||||
let status;
|
||||
match p3 {
|
||||
// Match can has guards exactly like in Python
|
||||
ProcessStatus::InProgress => {
|
||||
println!("In progress...");
|
||||
status = format!("In progress...");
|
||||
}
|
||||
ProcessStatus::Error(e) => {
|
||||
println!("Errorred {}", e);
|
||||
status = format!("Errorred {}", e);
|
||||
}
|
||||
ProcessStatus::Success {
|
||||
duration: d,
|
||||
output: o,
|
||||
} => {
|
||||
println!("Done in {:?} seconds: {}", d, o);
|
||||
status = format!("Done in {:?} seconds: {}", d, o);
|
||||
}
|
||||
}
|
||||
assert!(status == "Done in 25s seconds: Success");
|
||||
}
|
||||
|
||||
// The if let
|
||||
|
||||
fn test_if_let() {
|
||||
let p = ProcessStatus::Error(String::from("Timeout"));
|
||||
let mut status = "Unknown".to_owned();
|
||||
|
||||
match p {
|
||||
ProcessStatus::Error(ref e) => println!("Process failed: {e}"),
|
||||
ProcessStatus::Error(ref e) => status = format!("Process failed: {e}"),
|
||||
_ => (),
|
||||
}
|
||||
|
||||
// same as
|
||||
|
||||
if let ProcessStatus::Error(ref e) = p {
|
||||
println!("Process failed: {e}");
|
||||
status = format!("Process failed: {e}");
|
||||
}
|
||||
|
||||
assert!(status == "Process failed: Timeout");
|
||||
}
|
||||
|
||||
// No return annotation, inferred as an empty tuple, some kind of "void".
|
||||
pub fn main() {
|
||||
println!("Hello World");
|
||||
bools();
|
||||
chars();
|
||||
integers();
|
||||
|
@ -606,7 +610,6 @@ pub fn main() {
|
|||
play_with_enum(test);
|
||||
let test: Test<u64> = Test::C(1);
|
||||
play_with_enum(test);
|
||||
f_strings();
|
||||
durées();
|
||||
ownership();
|
||||
borrowing();
|
||||
|
|
11
src/jour2.rs
11
src/jour2.rs
|
@ -122,7 +122,7 @@ fn is_in(v: &Vec<&str>, needle: &str) -> bool {
|
|||
false
|
||||
}
|
||||
|
||||
fn print_first_item<T: std::fmt::Debug>(list: &[T]) {
|
||||
fn _print_first_item<T: std::fmt::Debug>(list: &[T]) {
|
||||
// Here list is "an iterable".
|
||||
dbg!(&list[0]);
|
||||
}
|
||||
|
@ -148,20 +148,19 @@ impl Iterator for Fib {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
println!("Hello World");
|
||||
play_with_generics_and_lifetime();
|
||||
let string1 = String::from("abcd");
|
||||
let string2 = "yux";
|
||||
|
||||
let result = longest(string1.as_str(), string2);
|
||||
println!("The longest string is {result}");
|
||||
assert!(result == "abcd");
|
||||
|
||||
let a = IPossesTheString {
|
||||
name: "Hello".to_owned(),
|
||||
};
|
||||
let b = IDontPossesIt { name: "Hello" };
|
||||
println!("{}", a.name);
|
||||
println!("{}", b.name);
|
||||
assert!(a.name == "Hello");
|
||||
assert!(b.name == "Hello");
|
||||
|
||||
// lambdas:
|
||||
|
||||
|
@ -201,7 +200,7 @@ pub fn main() {
|
|||
s[0] = 0;
|
||||
let v = vec![10, 20, 30];
|
||||
assert!(v[0] == 10);
|
||||
print_first_item(&v);
|
||||
//print_first_item(&v);
|
||||
|
||||
// HashSet
|
||||
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
mod bench;
|
||||
mod concurrency;
|
||||
mod doc;
|
||||
mod errors;
|
||||
mod exercises;
|
||||
mod jour1;
|
||||
mod jour2;
|
||||
|
@ -15,4 +17,6 @@ fn main() {
|
|||
starwars::main();
|
||||
tests::main();
|
||||
doc::main();
|
||||
errors::main();
|
||||
concurrency::main();
|
||||
}
|
||||
|
|
16
src/rpg.rs
16
src/rpg.rs
|
@ -64,11 +64,11 @@ impl Character {
|
|||
}
|
||||
|
||||
fn heal(&mut self, hp: u64) {
|
||||
print!("Before healing:");
|
||||
self.print();
|
||||
//print!("Before healing:");
|
||||
//self.print();
|
||||
self.hp = (self.hp + hp).min(100);
|
||||
print!("After healing:");
|
||||
self.print();
|
||||
//print!("After healing:");
|
||||
//self.print();
|
||||
}
|
||||
|
||||
fn paladin(name: &str) -> Self {
|
||||
|
@ -87,7 +87,7 @@ impl Character {
|
|||
character
|
||||
}
|
||||
|
||||
fn print(&self) {
|
||||
fn _print(&self) {
|
||||
match self.class {
|
||||
Class::Paladin => {
|
||||
println!("Paladin {} ({} hp)", self.name, self.hp);
|
||||
|
@ -104,15 +104,15 @@ impl Character {
|
|||
fn cast_spell(&mut self, other: &mut Character) {
|
||||
if let Class::Mage { mana } = self.class {
|
||||
if mana > 25 {
|
||||
println!("{} casting a spell to {}", self.name, other.name);
|
||||
//println!("{} casting a spell to {}", self.name, other.name);
|
||||
other.hp = (other.hp - 25).max(0);
|
||||
self.class = Class::Mage { mana: mana - 25 };
|
||||
} else {
|
||||
println!("Has not enoug mana!");
|
||||
//println!("Has not enoug mana!");
|
||||
}
|
||||
other.hp -= 50;
|
||||
} else {
|
||||
println!("{} cannot cast spell!", self.name);
|
||||
//println!("{} cannot cast spell!", self.name);
|
||||
self.hp = (self.hp - 5).max(0);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -71,10 +71,12 @@ pub fn main() {
|
|||
.collect::<Vec<_>>();
|
||||
with_bmi.sort_by_key(|tpl| ((tpl.1 * 1000.0) as u64));
|
||||
//dbg!(&with_bmi);
|
||||
let biggest_bmi;
|
||||
match with_bmi.last() {
|
||||
None => {}
|
||||
None => biggest_bmi = "??".to_owned(),
|
||||
Some(character) => {
|
||||
println!("{} has the biggest BMI", character.0);
|
||||
biggest_bmi = format!("{} has the biggest BMI", character.0);
|
||||
}
|
||||
}
|
||||
assert!(biggest_bmi == "Owen Lars has the biggest BMI");
|
||||
}
|
||||
|
|
10
src/tests.rs
10
src/tests.rs
|
@ -92,13 +92,15 @@
|
|||
// See also ntest, mockito, proptest.
|
||||
//
|
||||
mod my_module {
|
||||
#[allow(dead_code)]
|
||||
pub fn hello() {
|
||||
println!("Hello");
|
||||
todo!()
|
||||
}
|
||||
|
||||
pub mod nested_module {
|
||||
#[allow(dead_code)]
|
||||
pub fn bar() {
|
||||
println!("Glou");
|
||||
todo!()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -108,8 +110,8 @@ fn add<T: std::ops::Add<Output = T>>(x: T, y: T) -> T {
|
|||
}
|
||||
|
||||
pub fn main() {
|
||||
my_module::hello();
|
||||
my_module::nested_module::bar();
|
||||
//my_module::hello();
|
||||
//my_module::nested_module::bar();
|
||||
assert!(add(0.1, 0.2) == 0.2 + 0.1);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user