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

SC Homework Week 6

Link to original assignment


Chapter 08 and 09 Quizzes and Screenshots

8.1a 8 1a

This program does not compile, because the reference to the vector (&mut v) held by the for loop prevents simultaneous modification of the whole vector. So, while the values held by the vector v are mutable within the scope, v itself cannot be mutated.

8.1b 8 1b

This program compiles with the output 5 5, because v2 is a vector of mutable references, whereas vector v holds immutable 32-bit integer types. In the for-loop beginning on line 4, v2 is populated to references with each value in v, so when its value is updated, the values in v are as well.

8.2a 8 2a

Rust is a worry-ward, and since there are multiple ways UTF-8 strings can be interpreted, and none of these interpretations are "default", it refuses to come up with a "default" indexing operation.

8.2b 8 2b

References to string slices are references to byte sequences, but the difference between the two in rust &str and &[u8], is the guarantee that the string slice will always be valid UTF-8.

8.3a 8 3a

Because the immutable variable v1 borrows a value of h on line 4, h cannot be mutated until the end of v1's lifetime (?).

8.3b This 8 3b program compiles, and the output is 5, the sum of the two indices of 'l': 2 and 3. Apparently, if there are multiple results of the get() function, an iterable container is returned and can still be unwrapped life an option (intead of each i needing to be unwrapped).

8.3.3 8 3 3

This solution had the least overhead and was the computationally speediest. The vector is not cloned, and no new vector is instantiated within the function.

8.4.1 8 4 1

This was the best option becuase it didn't require the vector to be cloned, or for another vector to be instantiated and populated. Finally, this solution still uses a reference to the vector, preserving ownership.

8.4.2 8 4 2

The reason both of the selected options have undefined behavior is because each contained a vecotr with an element equal to 0. The option not selected only contained the value 1 for each element in the vector.

8.4.3 On line 8 4 3 5, the programmer attempts to make a references to the mutable

8.4.4 The compiler w 8 4 4 ill not allow our vector v to be mutably borrow twice in the same line.

8.4.5 8 4 5

This function causes an error when there's an odd number of elements in a vector because the program will try to access the same element when it reaches the middle. Because each of our options only has 2 elements, this isn't an issue - so none of them will caused undefined behavior. It is noted that one of the options stores the reference to the first item in vector v, then calls reverse on v while it's reference is still live. This would prevent compilation.

8.4.6 8 4 6

The selected option is the only one where the vector is not cloned, and no new vector is instantiated. Instead, it takes advantage of the unsafe keyword, which allows limited behaviors the compiler would otherwise reject.

9.1.1 9 1 1

The RUST_BACKTRACE variable allows us to see more information about what happened leading up to the error occurring.

9.1.2 9 1 2

A panic will effectively stop your program from running, so a panic should not be called within the program - it is meant to communicate a serious issue to the developer or user.

9.2.1 9 2 1

Originally I selected an option similar to this in which the arm Err(err) matched err, without returning. However, this answer is correct because the function will begin returning and then unwinding the remainder of the stack when an error is thrown.

9.2.2 9 2 2

If we had applied the .ok() method on the result type returned by File::open(), and the read_to_string result before using the ? operator, then this code would have compiled because it can only be used on Option types, not Result types.

9.3.1 9 3

Panic errors should only be thrown in cases where the error is unrecoverable - in this case, an incorrect user input is more appropriately corrected with output stating the parsing error, and the opportunity to retry input.

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