您的位置:首页 > 科技 > 能源 > 电脑公司网站源码_成都装修办公室公司_今日头条新闻10条_企业seo顾问服务阿亮

电脑公司网站源码_成都装修办公室公司_今日头条新闻10条_企业seo顾问服务阿亮

2025/3/13 6:28:05 来源:https://blog.csdn.net/htyu_0203_39/article/details/146162543  浏览:    关键词:电脑公司网站源码_成都装修办公室公司_今日头条新闻10条_企业seo顾问服务阿亮
电脑公司网站源码_成都装修办公室公司_今日头条新闻10条_企业seo顾问服务阿亮
struct S{data:String,
}//注意:因为String默认是移动语义,从而决定结构体S也是移动语义,可采用(1)或(2)两种方法解决编译错误;关键思路:放弃获取结构体S的字段data的所有权,改为借用。fn process(s_ref:&S){//&S ,借用match *s_ref { //S , 值//(1) match s_ref { //&S , 借用//(2) S{ref data} => { //data:&String , 借用S{data} => { //出错点.println!("Data: {}",data);},// _ => {},}
}fn main(){let s = S{data:String::from("hello world"),};process(&s);
}

编译错误:

   Compiling playground v0.0.1 (/playground)
error[E0507]: cannot move out of `s_ref.data` which is behind a shared reference--> src/main.rs:7:11|
7  |     match *s_ref { |           ^^^^^^
...
11 |         S{data} => {|           ----|           ||           data moved here|           move occurs because `data` has type `String`, which does not implement the `Copy` trait|
help: consider removing the dereference here|
7  -     match *s_ref { 
7  +     match s_ref { |For more information about this error, try `rustc --explain E0507`.
error: could not compile `playground` (bin "playground") due to 1 previous error

注意:个人水平有限,难免谬误,欢迎指正,仅做参考,抛砖引玉;怕日后遗忘,故随笔记录。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com