PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How to print an array and a vector?

 September 18, 2022     arrays, printing, rust, vector     No comments   

Issue

I'm playing with Rust and I wonder how I can print an array and a vector.

let a_vector = vec![1, 2, 3, 4, 5];
let an_array = ["a", "b", "c", "d", "e"];

I want to print on the screen and the result should be something like:

[1, 2, 3, 4, 5]
["a", "b", "c", "d", "e"]

In python it is:

lst = ["a", "b", "c", "d", "e"]
print lst

and printing it would show:

["a", "b", "c", "d", "e"]

Solution

println!("{:?}", a_vector);
println!("{:?}", an_array);

The {:?} is used to print types that implement the Debug trait. A regular {} would use the Display trait which Vec and arrays don't implement.



Answered By - A.B.
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing