@@ -106,6 +106,74 @@ class NumArray {
106
106
*/
107
107
```
108
108
109
+ ### ** Rust**
110
+
111
+ ``` rust
112
+ struct NumArray {
113
+ nums : Vec <i32 >
114
+ }
115
+
116
+
117
+ /**
118
+ * `&self` means the method takes an immutable reference.
119
+ * If you need a mutable reference, change it to `&mut self` instead.
120
+ */
121
+ impl NumArray {
122
+
123
+ fn new (nums : Vec <i32 >) -> Self {
124
+ Self {
125
+ nums
126
+ }
127
+ }
128
+
129
+ fn sum_range (& self , left : i32 , right : i32 ) -> i32 {
130
+ let (left , right ) = (left as usize , right as usize );
131
+ self . nums[left ..= right ]. iter (). sum :: <i32 >()
132
+ }
133
+ }
134
+
135
+ /**
136
+ * Your NumArray object will be instantiated and called as such:
137
+ * let obj = NumArray::new(nums);
138
+ * let ret_1: i32 = obj.sum_range(left, right);
139
+ */
140
+ ```
141
+
142
+ ``` rust
143
+ struct NumArray {
144
+ nums : Vec <i32 >
145
+ }
146
+
147
+
148
+ /**
149
+ * `&self` means the method takes an immutable reference.
150
+ * If you need a mutable reference, change it to `&mut self` instead.
151
+ */
152
+ impl NumArray {
153
+
154
+ fn new (mut nums : Vec <i32 >) -> Self {
155
+ let n = nums . len ();
156
+ for i in 1 .. n {
157
+ nums [i ] += nums [i - 1 ];
158
+ }
159
+ Self {
160
+ nums
161
+ }
162
+ }
163
+
164
+ fn sum_range (& self , left : i32 , right : i32 ) -> i32 {
165
+ let (left , right ) = (left as usize , right as usize );
166
+ self . nums[right ] - if left == 0 { 0 } else { self . nums[left - 1 ] }
167
+ }
168
+ }
169
+
170
+ /**
171
+ * Your NumArray object will be instantiated and called as such:
172
+ * let obj = NumArray::new(nums);
173
+ * let ret_1: i32 = obj.sum_range(left, right);
174
+ */
175
+ ```
176
+
109
177
### ** ...**
110
178
111
179
```
0 commit comments