Skip to main content

parse_str_as_bool

Function parse_str_as_bool 

Source
pub fn parse_str_as_bool(string: &str) -> Result<bool>
Expand description

Parses a string to a boolean value.

Accepts common boolean representations (case-insensitive):

  • true or "1"true
  • false or "0"false

§Arguments

  • string - The string to parse

§Returns

Returns Ok(bool) if the string is a valid boolean representation, or Err if the string cannot be parsed.

§Examples

assert_eq!(parse_str_as_bool("true").unwrap(), true);
assert_eq!(parse_str_as_bool("1").unwrap(), true);
assert_eq!(parse_str_as_bool("FALSE").unwrap(), false);
assert_eq!(parse_str_as_bool("0").unwrap(), false);
assert!(parse_str_as_bool("maybe").is_err());