SC_Homework2_Response - TheEvergreenStateCollege/upper-division-cs-23-24 GitHub Wiki

SC Homework Week 2

Link to original assignment


5.1.

The program compiles and the output will be 2, because on line 9 b takes ownership of a.x.
The program compiles and the output is 2 3, because by using the & operator, in lines 8 and 9 the value is being borrowed by making a referance. This is possible because p is mutable.

5.2

On line 8, rect1 is instantiated as an immutable object, then on line 13 a calls area(). The function doesn't take a reference as an argument, but a Rectangle object itself. This results in the ownership being passed to the function (I'm not sure if that's the right way to word that). As a result when rect1 is called again on line 14, the program would encounter an error preventing compilation.
Display and Debug are traits objects can implement, allowing them the functionality provided by the trait. Debug allows us to present errors, whereas Display allows us to print out the object. That's why Display is for an end-user, but Debug is for internal use.

5.3A

There isn't a constructor keyword, so the developer has the option to make their own. The context states that the function name "new" is a common choice.
This program doesn't compile. The implementation would have worked defined outside the main function, for the type Point, but not for the variable p which holds an instance of Point.

5.3B

The use of the &self parameter within a trait's function definition refers only to immutable refernces. However, this still compiles and allows us to find the length of the mutable vector v because the function is reborrowing the reference to the vector as an immutable reference, instead of a mutable one.
The use of a reference in the incr_v2 function is idiomatic, because it does not take ownership of that instance of Point.
The value of p.0 is 0 when p is instantiated, then the tuple item in index 0 is increased in the incr_x function which calls a mutable reference to self.
The variable x contains a mutable reference to the Point struct 'p' to access the value of x. This means that this reference is live when x is called on line 16, preventing access to p.y because x has now consumed ownership.

6.2

This passes the compiler. The second arm "Location::Range(_, n) => n" is true, so variable n takes the second value of the tuple, 5. This is printed.
Similar to unwrap, this function attempts to return an object wrapped within the Option type. Instead of throwing an error in the case of a None value, an action "other" is executed instead.
When the second match arm is called without the match statement using a reference to x, and without the use of a placeholder, ownership is consumed and x becomes inaccessible on the successive line.
Both functions have the same functionality, implemented with different syntax.

Chapter 6

6.1 6.2 6.3 6.1
6.1 Why?
6.2 Why?
6.3 Why?

6.3

In this case "if let" makes the most sense, since there is only one condition to check.
Since there are multiple values paired with unique actions (return statements), a match statement is the most suitable choice.

7.1

Packages contain crates. Crates contain modules.
main.rs, lib.rs and bin/alt.rs each produce creates. main.rs and bin/art.rs produce binary crates, however, and lib.rs produces a library crate.

7.2

Modules are effective for organization during compile-time. They do not speed up or effect runtime performance.

7.3

The crate is the root, so the start of the absolute path to an item in the current crate is either the name of the crate or the keyword crate.
The module bar is private, and while the function within it is marked public the scope of that visibility only applies within the scope of the private module.
Because module bar is public, the value of the public function b contained within bar is accessible from the root "foo".

7.4

The keyword "use" lets us refer to the path once, so that we may reduce verbosity in our code.
From main, we cannot access b. We can access a using the full path including the parent module (parent::a), or because a "use" statement is included, we can just refer to it directly: a. Because c is contained in child, and child is also given the alias 'alias', we can use three paths. parent::child::c, child::c, and finally alias::c.

7.5

Because foobar is the name of our package (visible at the root of the tree), and it contains the crate engine.rs which contains run, the resultant path is foobar::engine::run.
Chapter 5
5.1
5.2
5.3A
5.3B
Chapter 6
Chapter 7
7.1 7.2 7.3 7.4 7.5

📚 [return to diary homepage...]

⚠️ **GitHub.com Fallback** ⚠️