Borrow and AsRef
https://doc.rust-lang.org/book/borrow-and-asref.html
Borrow
とAsRef
について。
サンプルコードを動かしてみた。
use std::borrow::Borrow; use std::fmt::Display; fn foo<T: Borrow<i32> + Display>(a: T) { println!("a is borrowed: {}", a); } fn bar<T: AsRef<str>>(s: T) { let mut slice = s.as_ref(); println!("{}", slice); } fn main() { let mut i = 5; foo(&i); foo(&mut i); let s = "Hello".to_string(); bar(s); }
$ cargo run Finished debug [unoptimized + debuginfo] target(s) in 0.0 secs Running `target/debug/5_10_borrow_and_asref` a is borrowed: 5 a is borrowed: 5 Hello