Some updates to python and rust from reference project, started java section and go rpc/grpc sections

This commit is contained in:
2025-01-10 16:02:46 -05:00
parent 8bd8196841
commit 2b8ca93fab
15 changed files with 145 additions and 38 deletions

View File

@@ -7,6 +7,11 @@ Test functions are defined with the `#[test]` attribute. Within the tests, we u
For unit tests, its best to write your tests in the file that it is testing but contain it with a `mod tests` with the `cfg(test)` attribute. This will exclude it from the final executable or library. Unit tests also allow you to test private functions, meaning ones that are not `pub`.
```rust
#[allow(dead_code)]
pub fn greeting(_name: &str) -> String {
String::from("Hello!")
}
#[cfg(test)]
mod tests {
use super::*; // Use this to call functions in the current file
@@ -33,7 +38,31 @@ mod tests {
#[test]
#[should_panic]
fn greater_than_100() {
// Something illegal
panic!("I'm supposed to panic")
}
#[test]
#[should_panic(expected = "Expected text")]
fn expect_panic_message() {
panic!("Expected text");
}
// Ignore test
// Can still be run on demand
#[test]
#[ignore]
fn expensive_test() {
// code that takes an hour to run
}
// Return a result to allow for the ? operator to be used to cause test failures
#[test]
fn result_test() -> Result<(), String> {
if 2 + 2 == 4 {
Ok(())
} else {
Err(String::from("two plus two does not equal four"))
}
}
}
```

View File

@@ -91,7 +91,7 @@ fn some_function<T, U>(t: &T, u: &U) -> i32
## Returning types that implement traits
You can only use a trait as a return value if you are returning a single type.
You can only use a trait as a return value if you are returning a single type. For example, you can not have one path that returns a NewsArticle and one that returns a Tweet.
```rust
fn returns_summarizable() -> impl Summary {

View File

@@ -17,8 +17,11 @@ if num < 3 {
```rust
let config_max = Some(3u8);
let age: Result<u8, _> = "34".parse();
if let Some(max) = config_max {
println!("The maximum is configured to be {}", max);
} else if let Ok(age) = age {
println!("Age is {}", age);
}
```
@@ -107,10 +110,25 @@ fn value_in_cents(coin: Coin) -> u8 {
}
fn plus_one(x: Option<i32>) -> Option<i32> {
match x {
None => None,
Some(i) => Some(i + 1),
}
match x {
None => None,
Some(i) => Some(i + 1),
}
}
// Or
let x = 1;
match x {
1 | 2 => println!("one or two"),
3 => println!("three"),
_ => println!("anything"),
}
// Range
let x = 5;
match x {
1..=5 => println!("one through five"),
_ => println!("something else"),
}
```

View File

@@ -82,6 +82,21 @@ let _two = b[1];
// let n = a[10];
```
## Result
Results allow you to handle when something returns either a value or error. If it has a valid value, it will be of type `Ok<T>`, otherwise it will be of type `Err<E>`. See the `errors` section for more into on Results.
## Option
Options are similar to Results in that they can contain a conditional type. It can be either of type `None` or type `Some<T>`.
```rust
enum Option<T> {
None,
Some(T),
}
```
## Type Aliases
```rust