Is it possible to create a RefCell <Any>?

Is it possible to create something like this RefCell<Any>in Rust? I tried the following:

fn test2<T : Any>(x : T) -> RefCell<Any>{
    return RefCell::new(x) as RefCell<Any>
}

But I get the following error:

error: the trait `core::marker::Sized` is not implemented for the type `core::any::Any + 'static` [E0277]
<anon>:8 fn test2<T : Any>(x : T) -> RefCell<Any>{

The documentation for RefCellincludes the following

pub struct RefCell<T> where T: ?Sized {
    // some fields omitted
}

It makes me believe (along with the answer to this question) that this is possible. I also tried:

fn test1<T : Any>(x : T) -> Box<Any>{
    return Box::new(x) as Box<Any>
}

which works great. Both Box, and RefCellit seems, have the same boundaries, so I'm not quite sure what I am missing here. Any help is appreciated. I have this in Rust Playground , if useful.

+4
source share
2 answers

Box std::ops::CoerceUnsized, Box<Any>. RefCell , .

, :

let x = RefCell::new( String::new() );
let x = &x as &RefCell<Any>;

, RefCell<Any>, , .

+5

RefCell<Any> - ; - ? , Any, Box<Any>; RefCell<Any>, - Rc<RefCell<Any>>. RefCell<Box<Any>> , RefCell .

.

+3

Source: https://habr.com/ru/post/1628044/


All Articles