You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
See the discussion here. The Rc<str> may be the solution.
When there are many duplicated strings in the Vector, the &str type is much economic than String type, because the same strings can share one heap object.
Look at the example using String below:
fnmain(){let v:Vec<String> = vec!["foo".to_string(),"foo".to_string(),"bar".to_string(),];let a = v[0].as_ptr()as*const()asusize;let b = v[1].as_ptr()as*const()asusize;let c:f32 = 1.8;let _d = &c as*const_asusize;println!("v is {:?}", v);println!("a is {}, b is {}", a, b);println!("_d is {}", _d);}
The output is:
v is ["foo", "foo", "bar"]
a is 94313169717792, b is 94313169717824
_d is 140721848803820
The addresses of a and b are different, which implies that they are two different objects. And the address of _d is much smaller than a or b, which implies that _d is using stack memory and a and b are using heap memory.
And look at the &str example below:
fnmain(){let v:Vec<&str> = vec!["foo","foo","bar",];let a = v[0].as_ptr()as*const()asusize;let b = v[1].as_ptr()as*const()asusize;let c:f32 = 1.8;let _d = &c as*const_asusize;println!("v is {:?}", v);println!("a is {}, b is {}", a, b);println!("_d is {}", _d);}
And the output is as below:
v is ["foo", "foo", "bar"]
a is 94078134587479, b is 94078134587479
_d is 140729855478556
So they are actually pointing to the same object.
The text was updated successfully, but these errors were encountered:
So for ['foo', 'foo', 'bar', 'baz'], the vec is [0, 1, 1, 2] and the _map is {'foo': 0, 'bar': 1, 'baz': 2}. It seems that your solution is more elegant and needs less codes to maintain the StrList.
See the discussion here. The
Rc<str>
may be the solution.When there are many duplicated strings in the Vector, the
&str
type is much economic thanString
type, because the same strings can share one heap object.Look at the example using
String
below:The output is:
The addresses of
a
andb
are different, which implies that they are two different objects. And the address of_d
is much smaller thana
orb
, which implies that_d
is using stack memory anda
andb
are using heap memory.And look at the
&str
example below:And the output is as below:
So they are actually pointing to the same object.
The text was updated successfully, but these errors were encountered: