@@ -446,7 +446,7 @@ impl TypeId {
446446/// The current implementation uses the same infrastructure as compiler
447447/// diagnostics and debuginfo, but this is not guaranteed.
448448///
449- /// # Example
449+ /// # Examples
450450///
451451/// ```rust
452452/// assert_eq!(
@@ -459,3 +459,42 @@ impl TypeId {
459459pub const fn type_name < T : ?Sized > ( ) -> & ' static str {
460460 intrinsics:: type_name :: < T > ( )
461461}
462+
463+ /// Returns the name of the type of the pointed-to value as a string slice.
464+ /// This is the same as `type_name::<T>()`, but can be used where the type of a
465+ /// variable is not easily available.
466+ ///
467+ /// # Note
468+ ///
469+ /// This is intended for diagnostic use. The exact contents and format of the
470+ /// string are not specified, other than being a best-effort description of the
471+ /// type. For example, `type_name_of::<Option<String>>(None)` could return the
472+ /// `"Option<String>"` or `"std::option::Option<std::string::String>"`, but not
473+ /// `"foobar"`. In addition, the output may change between versions of the
474+ /// compiler.
475+ ///
476+ /// The type name should not be considered a unique identifier of a type;
477+ /// multiple types may share the same type name.
478+ ///
479+ /// The current implementation uses the same infrastructure as compiler
480+ /// diagnostics and debuginfo, but this is not guaranteed.
481+ ///
482+ /// # Examples
483+ ///
484+ /// Prints the default integer and float types.
485+ ///
486+ /// ```rust
487+ /// #![feature(type_name_of_val)]
488+ /// use std::any::type_name_of_val;
489+ ///
490+ /// let x = 1;
491+ /// println!("{}", type_name_of_val(&x));
492+ /// let y = 1.0;
493+ /// println!("{}", type_name_of_val(&y));
494+ /// ```
495+ #[ unstable( feature = "type_name_of_val" , issue = "66359" ) ]
496+ #[ rustc_const_unstable( feature = "const_type_name" ) ]
497+ pub const fn type_name_of_val < T : ?Sized > ( val : & T ) -> & ' static str {
498+ let _ = val;
499+ type_name :: < T > ( )
500+ }
0 commit comments