获得徽章 0
Rust 代码片段 Box<dyn Any>:
use std::any::Any;
fn print_if_string(s: Box<dyn Any>) {
if s.is::<String>() {
println!("It's a string...");
} else {
println!("Not a string...");
}
}
fn main() {
let new_string = "hello".to_string();
print_if_string(Box::new(new_string));
}
use std::any::Any;
fn print_if_string(s: Box<dyn Any>) {
if s.is::<String>() {
println!("It's a string...");
} else {
println!("Not a string...");
}
}
fn main() {
let new_string = "hello".to_string();
print_if_string(Box::new(new_string));
}
展开
评论
点赞
Rust 代码片段 -- dyn Any:
use std::any::Any;
fn print_if_string(s: &dyn Any) {
if s.is::<String>() {
println!("It's a string!");
} else {
println!("Not a string!");
}
}
fn main() {
print_if_string(&"hello".to_string());
}
use std::any::Any;
fn print_if_string(s: &dyn Any) {
if s.is::<String>() {
println!("It's a string!");
} else {
println!("Not a string!");
}
}
fn main() {
print_if_string(&"hello".to_string());
}
展开
评论
点赞
Rust 代码片段 -- 之 dyn Any(with TypeId):
use std::any::{Any, TypeId};
fn main() {
fn is_string(s: &dyn Any) {
if TypeId::of::<String>() == s.type_id() {
println!("It's a string!");
} else {
println!("Not a string!");
}
}
is_string(&"hello".to_string());
}
use std::any::{Any, TypeId};
fn main() {
fn is_string(s: &dyn Any) {
if TypeId::of::<String>() == s.type_id() {
println!("It's a string!");
} else {
println!("Not a string!");
}
}
is_string(&"hello".to_string());
}
展开
评论
点赞
赞了这篇文章
Rust 为 newtype 实现 Display
struct Meters(u32);
impl fmt::Display for Meters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
struct Meters(u32);
impl fmt::Display for Meters {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
}
}
展开
评论
点赞
使用create-react-app创建项目,别忘记支持typescript,状态管理mobx已经使用了n+1年了,请问还有比react-router更好用的路由管理么,sass也已经使用了n+1年了--没办法改变了,material-ui开始使用的时候大概还只是1.x。再配合bun+rsbuild,这应该是一个"值得纪念的时刻",因为未来的技术并非可以更好...
10
6
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("Got: {received}");
}
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
thread::spawn(move || {
let val = String::from("hi");
tx.send(val).unwrap();
});
let received = rx.recv().unwrap();
println!("Got: {received}");
}
展开
评论
点赞
css 背景图片自适应居中:
background-image: url("__url__");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
background-image: url("__url__");
background-repeat: no-repeat;
background-size: cover;
background-position: center;
评论
点赞