벡터에 저장된 문자열들의 첫 글자를 대문자로 변경하는 과제를 수행 중입니다. for 루프를 통해 각 요소에 접근하여 값을 수정하려고 합니다.
코드:
fn main() {
let mut words = vec![String::from("apple"), String::from("banana")];
for word in &words {
let mut upper_word = word.clone();
upper_word.make_ascii_uppercase();
*word = upper_word;
}
}
오류: error[E0594]: cannot assign to `*word`, which is behind a `&` reference
--> src/main.rs:6:9
|
6 | *word = upper_word;
| ^^^^^ cannot assign due to borrowed as immutable
시도: 1. `words` 변수를 `mut`으로 선언함.
2. `upper_word` 변수를 `mut`으로 선언함.
질문: `&words`를 통해 순회할 때 내부 요소를 직접 수정하려면 어떻게 참조자를 변경해야 합니까?
댓글3
모든 이야기1445
가벼운 안부부터 깊은 생각까지, 함께 나눠요.
총 1445개의 이야기
&words 말고 &mut words로 바꿔보세요
러스트 빌려쓰기 진짜 빡세네요ㅋㅋ