Skip to main content

Defining Generic Functions and Structs

  • Chapter
  • First Online:
Beginning Rust
  • 2877 Accesses

Abstract

Rust performs a strict data type check, so when you define a function that uses an argument of a certain type, say fn square_root(x: f32) -> f32, the code that invokes such a function must pass to it an expression of exactly that type, like in square_root(45.2f32), or it must perform explicit conversions every time that function is used, like in square_root(45.2f64 as f32). You cannot pass a different type, like in square_root(45.2f64).

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Chapter
USD 29.95
Price excludes VAT (USA)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
USD 39.99
Price excludes VAT (USA)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Author information

Authors and Affiliations

Authors

Rights and permissions

Reprints and permissions

Copyright information

© 2018 Carlo Milanesi

About this chapter

Check for updates. Verify currency and authenticity via CrossMark

Cite this chapter

Milanesi, C. (2018). Defining Generic Functions and Structs. In: Beginning Rust. Apress, Berkeley, CA. https://doi.org/10.1007/978-1-4842-3468-6_10

Download citation

Publish with us

Policies and ethics