You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+5-5Lines changed: 5 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -110,7 +110,7 @@ Not too scary, right?
110
110
111
111
This is a basic exponentiation algorithm that lets us find the final answer in logN time, and other than the `let`s and `mut`s, this doesn't read very differently from Python. The `mut` is just us telling Rust that the `result` and `base` objects can be changed after they're initially declared: Python lets you do that to anything at any time, but in Rust, things are immutable after their creation unless you say otherwise!
112
112
113
-
### Adding Step 2 of the Python function
113
+
### Extract and Transform
114
114
115
115
We're not going to touch the Rust helper function again from this point on: we're just going to process whatever data Python gives us into a format that our Rust-only function can use without complaining.
116
116
@@ -124,21 +124,21 @@ Finally, we flatten the nested vectors in to a single, 1-d vector. This is very
124
124
125
125
The explicit use of iterators is a language feature taken from functional programming, and it is virtually always faster, often much faster, than a regular for loop in Rust. Note also that because we used `.into_iter()` instead of `.iter()`, we consume the original nested_vecs value: from this line onwards, nested_vecs no longer exists in our program!
126
126
127
-
### Adding Step 3 of the Python function
127
+
### Matrix from Vec
128
128
129
129
Okay, that was a lot to take in, but it's all smooth sailing from here.
130
130
131
131
Now we're just creating a 2-d array using our newly flattened vector and the shape of the input matrix. We use the Array2 object's build-in-method to do this, but we handle the error case ourselves: if the dimensions of the vector don't match what we passed in, i.e. it can't be turned into a square matrix of size nxn, we return a Python ValueError, as before.
132
132
133
-
### Adding Step 4 of the Python function
133
+
### Call Rust Function
134
134
135
135
Now that we have everything in the desired types, we just call our Rust matrix exponentiation function directly: no sweat!
136
136
137
-
### Adding Step 5 of the Python function
137
+
### Transform Result
138
138
139
139
Now we're taking the matrix we got out and turning it back into a nested vector: we get the rows (which are an iterable), turn them into an iterator, turn each row into a separate vector, and then collect them into a vector of vectors. Done!
140
140
141
-
### Adding Step 6 of the Python function
141
+
### Return Output
142
142
143
143
Finally, we take this output and return it: we wrap it in `Ok()` to signal to the Rust compiler that this is a desired output, not an error.
0 commit comments