首页 > 其他 > 详细

rust中的ref

时间:2020-04-14 18:06:03      阅读:54      评论:0      收藏:0      [点我收藏+]

理解Rust的引用与借用(好文链接)

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    let ref a: i32;
    a = &1;
    let ref a: i32 = 1;
    print_type_of(a);

    let c: &u32 = &&&&&2;
    print_type_of(c);
}

上面2个a的类型都是&i32

enum带参数时使用match会move走enum的参数,如下这样写会报错

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    let os = Some(String::from("s"));
    match os {
      Some(s) => {
        print_type_of(s);
      },
      _ => ()
    };
    println!("{:?}", os);
}

改下match的参数匹配模式,用ref来匹配就不会出错了

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    let os = Some(String::from("s"));
    match os {
      Some(ref s) => {
        print_type_of(s); // s此时是&alloc::string::String类型
      },
      _ => ()
    };
    println!("{:?}", os);
}

如果match的对象是一个引用,会发现参数是引用类型,比如

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    let os = &Some(String::from("s"));
    match os {
      Some(s) => {
        print_type_of(s);
      },
      _ => ()
    };
}

打印的是&alloc::string::String

其实和下面的代码一样

#![feature(core_intrinsics)]
fn print_type_of<T>(_: T) {
    println!("{}", unsafe { std::intrinsics::type_name::<T>() });
}

fn main() {
    let os = &Some(String::from("s"));
    match os {
      &Some(ref s) => {
        print_type_of(s);
      },
      _ => ()
    };
}

 

rust中的ref

原文:https://www.cnblogs.com/chen8840/p/12699265.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!