Skip to content

Commit

Permalink
feat: add quiz for ch05-03 (cairo-book#783)
Browse files Browse the repository at this point in the history
Co-authored-by: Gift-Naomi <[email protected]>
Co-authored-by: enitrat <[email protected]>
  • Loading branch information
3 people authored Jun 1, 2024
1 parent 02205d8 commit bf27828
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
76 changes: 76 additions & 0 deletions quizzes/ch05-03-method-syntax.toml
Original file line number Diff line number Diff line change
@@ -0,0 1,76 @@
[[questions]]
type = "Tracing"
prompt.program = """
#[derive(Copy, Drop)]
struct Rectangle {
width: u64,
height: u64,
}
#[generate_trait]
impl RectangleImpl of RectangleTrait {
fn area(self: @Rectangle) -> u64 {
(*self.width) * (*self.height)
}
fn new(width: u64, height: u64) -> Rectangle {
Rectangle { width, height }
}
fn compare_areas(self: @Rectangle, r2: @Rectangle) -> bool {
self.area() == r2.area()
}
}
fn main() {
let rect1 = Rectangle {width: 40, height: 50};
let rect2 = RectangleTrait::new(10, 40);
println!("{}", rect1.compare_areas(@rect2));
}
"""

answer.doesCompile = true

answer.stdout = "false"
context = """
It compiles, because the type `Rectangle` on which we call the method on reference is implicitly passed as a `@Rectangle`
"""
id = "98bbc25c-80b2-4226-9219-a8d7b20fb991"


[[questions]]
type = "Tracing"
prompt.program = """
#[derive(Drop)]
struct Rectangle {
width: u64,
height: u64,
}
#[derive(Drop)]
struct Circle {
radius: u64,
}
trait RectangleTrait {
fn area(self: @Rectangle) -> u64;
}
impl RectangleImpl of RectangleTrait {
fn area(self: @Rectangle) -> u64 {
return (*self.width) * (*self.height);
}
}
fn main() {
let my_square = Rectangle { width: 30, height: 50 };
let my_circle = Circle { radius: 10 };
let area = my_circle.area();
println!("{}", area)
}
"""
answer.doesCompile = false
answer.lineNumber = 25
context = """
Methods can only be called on the types they're defined for; here, we try to call `area()` on `Circle`, when it's defined for `Rectangle`.
"""
id = "1e4a5bff-dc92-4c09-9f04-1d95fcf039bb"
2 changes: 2 additions & 0 deletions src/ch05-03-method-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,5 109,7 @@ the following code is equivalent to the code shown in the _Methods with several
There’s no strong reason to separate these methods into multiple `trait` and `impl`
blocks here, but this is valid syntax.

{{#quiz ../quizzes/ch05-03-method-syntax.toml}}

[enums]: ./ch06-01-enums.md
[modules]: ./ch07-02-defining-modules-to-control-scope.md

0 comments on commit bf27828

Please sign in to comment.