@@ -690,44 +690,41 @@ pub const fn type_name<T: ?Sized>() -> &'static str {
690
690
intrinsics:: type_name :: < T > ( )
691
691
}
692
692
693
- /// Returns the name of the type of the pointed-to value as a string slice.
693
+ /// Returns the type name of the pointed-to value as a string slice.
694
+ ///
694
695
/// This is the same as `type_name::<T>()`, but can be used where the type of a
695
696
/// variable is not easily available.
696
697
///
697
698
/// # Note
698
699
///
699
- /// This is intended for diagnostic use. The exact contents and format of the
700
- /// string are not specified, other than being a best-effort description of the
701
- /// type. For example, `type_name_of_val::<Option<String>>(None)` could return
702
- /// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
703
- /// `"foobar"`. In addition, the output may change between versions of the
704
- /// compiler.
705
- ///
706
- /// This function does not resolve trait objects,
707
- /// meaning that `type_name_of_val(&7u32 as &dyn Debug)`
708
- /// may return `"dyn Debug"`, but not `"u32"`.
700
+ /// Like [`type_name`], this is intended for diagnostic use and the exact output is not
701
+ /// guaranteed. It provides a best-effort description, but the output may change between
702
+ /// versions of the compiler.
709
703
///
710
- /// The type name should not be considered a unique identifier of a type;
711
- /// multiple types may share the same type name .
704
+ /// In short: use this for debugging, avoid using the output to affect program behavior. More
705
+ /// information is available at [`type_name`] .
712
706
///
713
- /// The current implementation uses the same infrastructure as compiler
714
- /// diagnostics and debuginfo, but this is not guaranteed.
707
+ /// Additionally, this function does not resolve trait objects. This means that
708
+ /// `type_name_of_val(&7u32 as &dyn Debug)` may return `"dyn Debug"`, but wil not return `"u32"`
709
+ /// at this time.
715
710
///
716
711
/// # Examples
717
712
///
718
713
/// Prints the default integer and float types.
719
714
///
720
715
/// ```rust
721
- /// #![feature(type_name_of_val)]
722
716
/// use std::any::type_name_of_val;
723
717
///
724
- /// let x = 1;
725
- /// println!("{}", type_name_of_val(&x));
726
- /// let y = 1.0;
727
- /// println!("{}", type_name_of_val(&y));
718
+ /// let s = "foo";
719
+ /// let x: i32 = 1;
720
+ /// let y: f32 = 1.0;
721
+ ///
722
+ /// assert!(type_name_of_val(&s).contains("str"));
723
+ /// assert!(type_name_of_val(&x).contains("i32"));
724
+ /// assert!(type_name_of_val(&y).contains("f32"));
728
725
/// ```
729
726
#[ must_use]
730
- #[ unstable ( feature = "type_name_of_val" , issue = "66359 " ) ]
727
+ #[ stable ( feature = "type_name_of_val" , since = "CURRENT_RUSTC_VERSION " ) ]
731
728
#[ rustc_const_unstable( feature = "const_type_name" , issue = "63084" ) ]
732
729
pub const fn type_name_of_val < T : ?Sized > ( _val : & T ) -> & ' static str {
733
730
type_name :: < T > ( )
0 commit comments