liveBook · Manning

Method call

As explained in Rust in Action: Systems programming concepts and techniques

A method call is an expression that invokes a method associated with a value or type.

A method call invokes behavior through a value or type, using dot syntax such as `record.trim()`, `record.split(',')`, or `1.2_f32.ceil()`. section 1.5 section 5.4.4

Method calls appear in several practical forms:

  • Chain calls to process data, as in `record.trim().len()` and `record.split(',').map(...).collect()`. section 1.5
  • Call methods on numeric values, as in `1.2_f32.ceil()` and `2_f32.powf(...)`, instead of passing values to standalone functions. section 5.4.4
  • Use parentheses when unary minus could be confused with method-call precedence: `(-1.0_f32).powf(0.0)` is explicit, while `-1.0_f32.powf(0.0)` is parsed differently. section 5.4.4

Processing a CSV record

A record can be trimmed, split, transformed, and collected through a readable chain: `record.trim().split(',').map(|field| field.trim()).collect()`. The resulting fields are then indexed for later processing. section 1.5

Listing 1.2 Example of Rust code showing some basic processing of CSV data
 fn main() {                                           #1
   let penguin_data = "\                               #2
   common name,length (cm)
   Little penguin,33
   Yellow-eyed penguin,65
   Fiordland penguin,60
   Invalid,data
   ";
 
   let records = penguin_data.lines();
 
   for (i, record) in records.enumerate() {
     if i == 0 || record.trim().len() == 0 {           #3
       continue;
     }
 
     let fields: Vec<_> = record                       #4
       .split(',')                                     #5
       .map(|field| field.trim())                      #6
       .collect();                                     #7
     if cfg!(debug_assertions) {                       #8
       eprintln!("debug: {:?} -> {:?}",
              record, fields);                         #9
     }
 
     let name = fields[0];
     if let Ok(length) = fields[1].parse::<f32>() {    #10
         println!("{}, {}cm", name, length);           #11
     }
   }
 }

Inspect the complete CSV-processing program to see method calls embedded in control flow, closure-based transformation, collection, and output. section 1.5

How Rust in Action: Systems programming concepts and techniques covers it

How deep it goes
Explains dot syntax through working examples and operator precedence, but does not develop method implementation or dispatch internals.
Examples
Demonstrates CSV parsing with `trim`, `split`, `map`, and `collect`, plus rounding, exponentiation, and floating-point bit decoding.
What it assumes
Assumes basic Rust expressions and types while introducing method syntax through accessible text-processing and numeric examples.
What you can do after
Apply method syntax to transform text, call numeric operations, and recognize precedence problems in Rust expressions.
How it connects
Connects method calls with closures, vectors, iterators, numeric types, operator precedence, and floating-point representation.

Also explained in

Compare how 2 books explain method call →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Rust in Action: Systems programming concepts and techniques. The section links point to the parts of the book it draws on.