Skip to content

Commit e8bd42c

Browse files
committed
Improved description
1 parent 2130bbb commit e8bd42c

File tree

1 file changed

+43
-2
lines changed

1 file changed

+43
-2
lines changed

library/core/src/hint.rs

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -512,11 +512,23 @@ pub const fn must_use<T>(value: T) -> T {
512512
value
513513
}
514514

515-
/// Hints to the compiler that branch condition is likely to be true.
515+
/// Hints to the compiler that a branch condition is likely to be true.
516516
/// Returns the value passed to it.
517517
///
518518
/// It can be used with `if` or boolean `match` expressions.
519519
///
520+
/// When used outside of a branch condition, it may still work if there is a branch close by, but
521+
/// it is not guaranteed to have any effect.
522+
///
523+
/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
524+
/// compound expressions, such as `likely(a && b)`. When applied to compound expressions, it has
525+
/// the following effect:
526+
/// ```
527+
/// likely(!a) => !unlikely(a)
528+
/// likely(a && b) => likely(a) && likely(b)
529+
/// likely(a || b) => a || likely(b)
530+
/// ```
531+
///
520532
/// # Examples
521533
///
522534
/// ```
@@ -534,20 +546,42 @@ pub const fn must_use<T>(value: T) -> T {
534546
/// true => println!("this branch is likely to be taken"),
535547
/// false => println!("this branch is unlikely to be taken"),
536548
/// }
549+
///
550+
/// // Use outside of a branch condition. This may still work if there is a branch close by,
551+
/// // but it is not guaranteed to have any effect
552+
/// let cond = likely(x != 0);
553+
/// if cond {
554+
/// println!("this branch is likely to be taken");
555+
/// }
537556
/// }
538557
/// ```
558+
///
559+
///
560+
539561
#[unstable(feature = "likely_unlikely", issue = "26179")]
540562
#[rustc_nounwind]
541563
#[inline(always)]
542564
pub const fn likely(b: bool) -> bool {
543565
crate::intrinsics::likely(b)
544566
}
545567

546-
/// Hints to the compiler that the branch condition is unlikely to be true.
568+
/// Hints to the compiler that a branch condition is unlikely to be true.
547569
/// Returns the value passed to it.
548570
///
549571
/// It can be used with `if` or boolean `match` expressions.
550572
///
573+
/// When used outside of a branch condition, it may still work if there is a branch close by, but
574+
/// it is not guaranteed to have any effect.
575+
///
576+
/// It can also be applied to parts of expressions, such as `likely(a) && unlikely(b)`, or to
577+
/// compound expressions, such as `unlikely(a && b)`. When applied to compound expressions, it has
578+
/// the following effect:
579+
/// ```
580+
/// unlikely(!a) => !likely(a)
581+
/// unlikely(a && b) => a && unlikely(b)
582+
/// unlikely(a || b) => unlikely(a) || unlikely(b)
583+
/// ```
584+
///
551585
/// # Examples
552586
///
553587
/// ```
@@ -565,6 +599,13 @@ pub const fn likely(b: bool) -> bool {
565599
/// true => println!("this branch is unlikely to be taken"),
566600
/// false => println!("this branch is likely to be taken"),
567601
/// }
602+
///
603+
/// // Use outside of a branch condition. This may still work if there is a branch close by,
604+
/// // but it is not guaranteed to have any effect
605+
/// let cond = unlikely(x != 0);
606+
/// if cond {
607+
/// println!("this branch is likely to be taken");
608+
/// }
568609
/// }
569610
/// ```
570611
#[unstable(feature = "likely_unlikely", issue = "26179")]

0 commit comments

Comments
 (0)