Skip to main content
St Louis

Back to all posts

How to Convert Char** to Vec<String> In Rust?

Published on
3 min read
How to Convert Char** to Vec<String> In Rust? image

Best Rust Programming Books to Buy in September 2025

1 The Rust Programming Language, 2nd Edition

The Rust Programming Language, 2nd Edition

BUY & SAVE
$30.13 $49.99
Save 40%
The Rust Programming Language, 2nd Edition
2 Programming Rust: Fast, Safe Systems Development

Programming Rust: Fast, Safe Systems Development

BUY & SAVE
$43.99 $79.99
Save 45%
Programming Rust: Fast, Safe Systems Development
3 Rust for Rustaceans: Idiomatic Programming for Experienced Developers

Rust for Rustaceans: Idiomatic Programming for Experienced Developers

BUY & SAVE
$29.99 $49.99
Save 40%
Rust for Rustaceans: Idiomatic Programming for Experienced Developers
4 Rust in Action

Rust in Action

BUY & SAVE
$51.42 $59.99
Save 14%
Rust in Action
5 Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)

BUY & SAVE
$47.04 $59.95
Save 22%
Rust Programming: A Practical Guide to Fast, Efficient, and Safe Code with Ownership, Concurrency, and Web Programming (Rheinwerk Computing)
6 Zero To Production In Rust: An introduction to backend development

Zero To Production In Rust: An introduction to backend development

BUY & SAVE
$49.99
Zero To Production In Rust: An introduction to backend development
+
ONE MORE?

To convert a char** to a Vec<String> in Rust, you can use the following steps:

  1. Collect the C-style strings into CStr slices:

use std::ffi::CStr;

let c_strs: Vec<&CStr> = (0..size).map(|i| { unsafe { CStr::from_ptr(char_double_ptr[i]) } }).collect();

  1. Convert CStr slices to Rust String objects:

let rust_strings: Vec = c_strs.iter().map(|&c_str| { c_str.to_string_lossy().to_string() }).collect();

Here, size is the size of the char** array.

Note: Conversion from C-style strings to String might result in lossy conversions if the strings contain non-UTF-8 characters.

How to pass vec as function arguments in Rust?

In Rust, you can pass a Vec as a function argument by specifying it as a parameter with the & or &mut reference types.

Here's an example of passing a Vec by reference:

fn do_something_with_vec(data: &Vec) { // Access the values of the Vec for num in data { println!("{}", num); } }

fn main() { let numbers = vec![1, 2, 3, 4, 5]; do_something_with_vec(&numbers); }

In this example, the do_something_with_vec function accepts an immutable reference to a Vec<i32> using the & symbol. It iterates over the elements of the Vec using a for loop.

To pass a mutable reference, you can use the &mut symbol:

fn do_something_with_vec(data: &mut Vec) { // Modify the values of the Vec for num in data { *num *= 2; } }

fn main() { let mut numbers = vec![1, 2, 3, 4, 5]; do_something_with_vec(&mut numbers);

println!("{:?}", numbers);

}

In this case, the do_something_with_vec function accepts a mutable reference to a Vec<i32> using the &mut symbol. It multiplies each element of the Vec by 2.

Make sure to pass the reference when calling the function using the & or &mut syntax, otherwise a move error will occur.

How to append elements to a vec in Rust?

To append elements to a vector in Rust, you can use the push() method or the extend() method. Here's an example of how to use both methods:

fn main() { // Creating an empty vector let mut vector: Vec = Vec::new();

// Appending a single element using push()
vector.push(1);

// Appending multiple elements using extend()
vector.extend(\[2, 3, 4\].iter().cloned());

println!("{:?}", vector); // Output: \[1, 2, 3, 4\]

}

In this example, we first create an empty vector vector using Vec::new(). Then, we use the push() method to append the element 1 to the vector. Finally, we use the extend() method along with an iterator to append the elements 2, 3, and 4 to the vector.

Both push() and extend() methods take ownership of the elements being appended. If you want to append references or borrow the elements, you need to use the & or &mut prefixes.

What is the difference between char** and vec?

char** and vec are completely different types and serve different purposes. Here's a brief explanation of each:

  1. char** (Character Pointer to Pointer): char** is a pointer to a pointer that points to a character or a sequence of characters in memory. It is often used to represent a dynamically allocated 2D array of characters (i.e., a string array).
  2. vec (Vector): vec generally refers to a vector in programming languages that support it as a standard container type (e.g., C++, Python). It is a dynamic array-like data structure that can hold elements of any type (e.g., integers, strings, custom objects). Vectors provide dynamic resizing, efficient indexing, and various operations like insertion, deletion, and iteration.

In summary, char** is specifically used for handling arrays of characters, while vec is a more general-purpose dynamic array-like container that can hold any type of elements.