-
-
Notifications
You must be signed in to change notification settings - Fork 12
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
Showing
5 changed files
with
184 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
# Hints | ||
|
||
## General | ||
|
||
- [The Cairo Book: ByteArrays][book-bytearrays] | ||
- [Starknet By Example: Strings & ByteArrays][sbe-strings] | ||
|
||
### 1. Format a magical chant | ||
|
||
- Use the `format!` macro to combine multiple `ByteArray` inputs into a single string. | ||
- Separate the chants with a delimiter such as `"-"` to match the expected output. | ||
- Ensure the output is returned as a `ByteArray`. | ||
|
||
### 2. Implement a `Display` trait for `EchoStone` | ||
|
||
- The `Display` trait allows you to define how the `EchoStone` should appear when printed with `{}`. | ||
- Use the `write!` macro within the `fmt` function to format the output string. | ||
- Include both `power` and `duration` fields in a clear and concise format, e.g., `EchoStone [power: X, duration: Y]`. | ||
|
||
### 3. Implement a `Debug` trait for `EchoStone` | ||
|
||
- The `Debug` trait is used for debugging purposes, allowing for more detailed or structured output when printed with `{:?}`. | ||
- Follow a clear debugging format, such as `Debugging EchoStone: { power: X, duration: Y }`. | ||
- Use the `write!` macro to create the debug output. | ||
|
||
[book-bytearrays]: https://book.cairo-lang.org/ch02-02-data-types.html#string-types | ||
[sbe-strings]: https://starknet-by-example.voyager.online/getting-started/basics/bytearrays-strings |
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,46 @@ | ||
# Introduction | ||
|
||
Printing in Cairo allows you to display messages or debug information during program execution. | ||
|
||
## Basics | ||
|
||
Cairo provides two macros for printing: | ||
|
||
- `println!`: Outputs a message followed by a newline. | ||
- `print!`: Outputs a message without a newline. | ||
|
||
```rust | ||
println!("Hello, Cairo!"); | ||
println!("x = {}, y = {}", 10, 20); | ||
``` | ||
|
||
Placeholders `{}` are replaced with provided values. | ||
|
||
## Formatting Strings | ||
|
||
Use `format!` to create a `ByteArray` without immediately printing: | ||
|
||
```rust | ||
let result = format!("{}-{}-{}", "tic", "tac", "toe"); | ||
println!("{}", result); // Output: tic-tac-toe | ||
``` | ||
|
||
## Custom Data Types | ||
|
||
For custom types, implement `Display` or derive `Debug` for printing: | ||
|
||
```rust | ||
#[derive(Debug)] | ||
struct Point { x: u8, y: u8 } | ||
|
||
let p = Point { x: 3, y: 4 }; | ||
println!("{:?}", p); // Debug output: Point { x: 3, y: 4 } | ||
``` | ||
|
||
## Hexadecimal Printing | ||
|
||
Use `{:x}` to print integers as hexadecimal: | ||
|
||
```rust | ||
println!("{:x}", 255); // Output: ff | ||
``` |
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
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,36 @@ | ||
# Design | ||
|
||
## Goal | ||
|
||
Introduce students to the use of formatting traits like `Display` and `Debug` and how to concatenate strings effectively using the `format!` macro. | ||
|
||
## Learning Objectives | ||
|
||
- Understand and implement the `Display` trait for custom types. | ||
- Understand and implement the `Debug` trait for custom types. | ||
- Learn how to use the `format!` macro to concatenate strings and format output. | ||
- Explore the differences between the `Display` and `Debug` traits in formatting. | ||
|
||
## Out of Scope | ||
|
||
- Advanced string manipulation techniques. | ||
- Error handling mechanisms beyond basic usage. | ||
- Interaction with non-primitive custom types within formatting. | ||
|
||
## Concepts | ||
|
||
- printing | ||
|
||
## Prerequisites | ||
|
||
- traits | ||
- strings | ||
- structs | ||
|
||
## Resources to Refer To | ||
|
||
- [Cairo Book - Printing][printing] | ||
- [Cairo Book - Macros][macros] | ||
|
||
[printing]: https://book.cairo-lang.org/ch11-08-printing.html | ||
[macros]: https://book.cairo-lang.org/ch11-05-macros.html |
83 changes: 74 additions & 9 deletions
83
exercises/concept/the-realm-of-echoes/tests/the_realm_of_echoes.cairo
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 |
---|---|---|
@@ -1,21 +1,86 @@ | ||
use the_realm_of_echoes::{EchoStone, format_magical_chant}; | ||
|
||
const U32_MAX: u32 = 0xFFFFFFFF; | ||
|
||
#[test] | ||
fn format_magical_chant_basic() { | ||
let chant1 = "abra"; | ||
let chant2 = "cadabra"; | ||
let chant3 = "alakazam"; | ||
assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra-cadabra-alakazam"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn format_magical_chant_empty_strings() { | ||
let chant1 = ""; | ||
let chant2 = ""; | ||
let chant3 = ""; | ||
assert_eq!(format_magical_chant(chant1, chant2, chant3), "--"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn format_magical_chant_mixed_empty_and_non_empty() { | ||
let chant1 = "abra"; | ||
let chant2 = ""; | ||
let chant3 = "alakazam"; | ||
assert_eq!(format_magical_chant(chant1, chant2, chant3), "abra--alakazam"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn format_magical_chant_with_special_characters() { | ||
let chant1 = "ab@ra"; | ||
let chant2 = "ca!dabra"; | ||
let chant3 = "ala#kazam"; | ||
assert_eq!(format_magical_chant(chant1, chant2, chant3), "ab@ra-ca!dabra-ala#kazam"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn echo_stone_display_basic() { | ||
let stone = EchoStone { power: 100, duration: 50 }; | ||
assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 50]"); | ||
} | ||
|
||
#[test] | ||
fn test_format_magical_chant() { | ||
let result = format_magical_chant("Spark", "Shine", "Glow"); | ||
assert_eq!(result, "Spark-Shine-Glow"); | ||
#[ignore] | ||
fn echo_stone_display_zero_values() { | ||
let stone = EchoStone { power: 0, duration: 0 }; | ||
assert_eq!(format!("{stone}"), "EchoStone [power: 0, duration: 0]"); | ||
} | ||
|
||
|
||
#[test] | ||
#[ignore] | ||
fn echo_stone_display_large_values() { | ||
let stone = EchoStone { power: U32_MAX, duration: U32_MAX }; | ||
assert_eq!( | ||
format!("{stone}"), format!("EchoStone [power: {}, duration: {}]", U32_MAX, U32_MAX), | ||
); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn echo_stone_debug_basic() { | ||
let stone = EchoStone { power: 100, duration: 50 }; | ||
assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 100, duration: 50 }"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_stringify_echo_stone() { | ||
let stone = EchoStone { power: 100, duration: 300 }; | ||
assert_eq!(format!("{stone}"), "EchoStone [power: 100, duration: 300]"); | ||
fn echo_stone_debug_zero_values() { | ||
let stone = EchoStone { power: 0, duration: 0 }; | ||
assert_eq!(format!("{stone:?}"), "Debugging EchoStone: { power: 0, duration: 0 }"); | ||
} | ||
|
||
#[test] | ||
#[ignore] | ||
fn test_debug_echo_stone() { | ||
let stone = EchoStone { power: 100, duration: 300 }; | ||
assert_eq!(format!("{:?}", stone), "Debugging EchoStone: { power: 100, duration: 300 }"); | ||
fn echo_stone_debug_large_values() { | ||
let stone = EchoStone { power: U32_MAX, duration: U32_MAX }; | ||
assert_eq!( | ||
format!("{stone:?}"), | ||
format!("Debugging EchoStone: {{ power: {}, duration: {} }}", U32_MAX, U32_MAX), | ||
); | ||
} |