Rust's len() vs Python's: a method vs a function

While learning Rust, i found something odd about len(). In Python, len() is a global function. Return the length (the number of items) of an object. The argument may be a sequence (such as a string, bytes, tuple, list, or range) or a collection (such as a dictionary, set, or frozen set). Fun fact: it raises OverflowError for lengths exceeding sys.maxsize. such as range(2 ** 100). Rust:len() is a method on types like String, Vec, etc, not a global function. For strings (String, &str): len() gives you bytes, not characters count. For actual character count, you’ll need .chars().count(). For collections (Vec, arrays, HashMap): len() counts elements, as expected. Also, Rust's len() always returns usize (unsigned integer), so no negative lengths or overflow errors like Python! Different philosophies, same three letters.... #rust #python #len #iced #dev

To view or add a comment, sign in

Explore content categories