@@ -512,11 +512,23 @@ pub const fn must_use<T>(value: T) -> T {
512
512
value
513
513
}
514
514
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.
516
516
/// Returns the value passed to it.
517
517
///
518
518
/// It can be used with `if` or boolean `match` expressions.
519
519
///
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
+ ///
520
532
/// # Examples
521
533
///
522
534
/// ```
@@ -534,20 +546,42 @@ pub const fn must_use<T>(value: T) -> T {
534
546
/// true => println!("this branch is likely to be taken"),
535
547
/// false => println!("this branch is unlikely to be taken"),
536
548
/// }
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
+ /// }
537
556
/// }
538
557
/// ```
558
+ ///
559
+ ///
560
+
539
561
#[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
540
562
#[ rustc_nounwind]
541
563
#[ inline( always) ]
542
564
pub const fn likely ( b : bool ) -> bool {
543
565
crate :: intrinsics:: likely ( b)
544
566
}
545
567
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.
547
569
/// Returns the value passed to it.
548
570
///
549
571
/// It can be used with `if` or boolean `match` expressions.
550
572
///
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
+ ///
551
585
/// # Examples
552
586
///
553
587
/// ```
@@ -565,6 +599,13 @@ pub const fn likely(b: bool) -> bool {
565
599
/// true => println!("this branch is unlikely to be taken"),
566
600
/// false => println!("this branch is likely to be taken"),
567
601
/// }
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
+ /// }
568
609
/// }
569
610
/// ```
570
611
#[ unstable( feature = "likely_unlikely" , issue = "26179" ) ]
0 commit comments