P<SomeStruct>
(소유 또는 변경 가능한 참조) 유형의 변수가있는 경우 새 포인터를 반환하지 않고 해당 구조체의 필드를 변경할 수 있습니까? 나는 이런 식으로 뭔가를하려고했는데 : 스마트 포인터 내에서 구조체의 필드를 변이 어떤 방법이 있나요포인터 내에서 struct의 필드를 변경하는 방법은 무엇입니까?
error: cannot assign to immutable field
--> src/lib.rs:116:5
|
116 | x.id = "bing".to_string()
| ^^^^^^^^^^^^^^^^^^^^^^^^^ cannot mutably borrow immutable field
:
#![feature(rustc_private)]
extern crate syntax;
use syntax::ptr::P;
#[derive(Debug)]
struct Baz {
id: String,
}
#[test]
fn foo() {
let mut pointer = P(Baz { id: "blah".to_string() });
bar(&mut pointer);
}
fn bar(x: &mut P<Baz>) {
x.id = "bing".to_string()
}
하지만 실패 물론
?놀이터 URL : https://play.rust-lang.org/?gist=5675bc2ef4297fe691204a69ffc19461&version=nightly&backtrace=0
이것은 재현 할 수 없습니다. 이 정확한 컴파일 오류를 보여주는 [Rust Playground] (https://play.rust-lang.org)에 예제를 작성해보십시오. –
@ E_net4, 필요한 수입 및 기능 플래그와 녹 놀이터에 대한 링크를 추가했습니다. –